Link to home
Start Free TrialLog in
Avatar of TookH
TookH

asked on

ExitThread memory leaks?

I was looking up CreateThread in my Win32 documentation when I noticed a little paragraph at the bottom:

"A thread that uses functions from the C run-time libraries should use the beginthread and endthread C run-time functions for thread management rather than CreateThread and ExitThread. Failure to do so results in small memory leaks when ExitThread is called."

Is this true? The "For example, see Creating Threads" link on the page leads to an example program that uses wsprintf in the thread. Kind of hypocritical.

If it's true, then does it mean that I have to implement copies of the CRT functions that I use?
ASKER CERTIFIED SOLUTION
Avatar of NickRepin
NickRepin

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of NickRepin
NickRepin

I meant to say _beginthread() and _beginthreadex(). The latter one is almost the same as CreateThread() with regard to the parameters.

The difference is that you can use any C function inside the thread. To terminate the "C" thread simply return or call _endthread() or _endthreadex().
Avatar of TookH

ASKER

I find this really disturbing! I've seen so many sample programs that use CreateThread and CRT functions. Does MFC's thread class use CreateThread or beginthread? How significant are the leaks?
>>Does MFC's thread class use CreateThread or beginthread?
Sure, beginthread. See "C:\Program Files\Microsoft Visual Studio\VC98\MFC\SRC\THRDCORE.CPP"

>>How significant are the leaks?
Well, it is not disturbing at all to call beginthread instead of CreateThread. beginthread initializes some thread-related memory; with CreateThread some of RTL functions might work incorrectly or not work at all.
Avatar of TookH

ASKER

I suppose I should just get used to using beginthread, then. It makes me angry, though. What is the point of putting a function in a library if it causes bugs in 90% of the programs it's used in?

Are there events, critical sections, ect. for use with beginthread?