Thread handle leaks


By carefully reading
you can see begin a thread created with _beginthreadex must be closed by _endthreadex. _beginthread can’t be used because “If the thread generated by _beginthread exits quickly, the handle returned to the caller of _beginthread might be invalid or, worse, point to another thread” (sight)
Secondly, _endthreadex doesn’t automatically close the created handle.
This is kind of problematic with the way juce implements it (much probably because of the win32 brilliant implementation).
when threadEntryProc calls _endthread the thread is already finished and there is no way to close the handle properly, so we had to write an ugly fix by using a static list of handles to close (…) I hope you’ll find a better way to do this.

To find handle leaks, you can use the task manager, and make it show the handle cound in the column selection dialog.
To find out more precisely what kind of handles these are you can use handle.exe :
you can use it this way :
handle.exe -s -p jucer

We found out there are other handle leaks, mostly Event handles, but we weren’t able to find out where these are from, cause it looks like every CreateEvent() call is matched with a CloseHandle()…

Interesting… Yes, it does seem that the _endthread call doesn’t do its job properly - very annoying! And what a badly written load of rubbish the help is for those functions!

I’ve had a play around with it, and have checked in a version that now does seem to close the handles properly - running the threads page of the jucedemo and watching the handle count is quite a good test, as it’s creating and deleting threads all the time.

No idea about the other leaked handles, but I’m pretty confident they’re not juce ones - I’ve added some names to the events now, so in ProcessExplorer you can see which ones are juce waitableevents, and they don’t seem to get leaked. My guess is that any leaks you’ve got must be coming from things like audio devices or other modules. Can’t think of any other way it could happen.

ok thanks !

I didn’t know about the ProcessExplorer tool, it should give me more valuable information next time I’ll be looking for these damn leaks !