William A. Rowe, Jr. wrote:
> Chris Storah wrote:
>
>> Enclosed is a patch to fix a leak in APR threads, due to _endthreadex
>> not automatically closing the handle (unlike _endthread).
>
>
> Chris,
>
> this is facinating, thank you for bringing it to our attention!!!
>
There are couple of more things:
1. dummy_worker must be __stdcall.
2. dummy_worker must call _endthreadex:
now its:
apr_thread_t *thd = (apr_thread_t *)opaque;
TlsSetValue(tls_apr_thread, thd->td);
return thd->func(thd, thd->data);
it should be:
void *rv;
apr_thread_t *thd = (apr_thread_t *)opaque;
TlsSetValue(tls_apr_thread, thd->td);
rv = thd->func(thd, thd->data);
TlsSetValue(tls_apr_thread, NULL);
if (thd->td)
_endthreadex(0);
return rv;
This will ensure that either on thread_join and thread_exit
both handle and per thread CRT data gets released.
Now we have a situation where _beginthreadex->CloseHandle
can be executed, while we should for each _beginthreadex
have corresponding _endthreadex. This can happen in
situations where we call thread_join on threads that
does not call the thread_exit before returning.
Regards,
Mladen.