mfc_speak
asked on
AfxEndThread causes memory leaks
I have a worker thread running. I have a CString object created on stack frame. When I exit the thread prematurely, using AfxEndThread(1) I get memory leaks in debug window. But if I do a return 1, I do not get any memory leaks . Why?
ASKER CERTIFIED SOLUTION
membership
Create a free account to see this answer
Signing up is free and takes 30 seconds. No credit card required.
ASKER
Hi,
Should I use AfxEndThread(1) or return 1 to exit a thread prematurely?
Should I use AfxEndThread(1) or return 1 to exit a thread prematurely?
personally I only use AfxEndThread when I need to exit a thread quickly due to a serious problem or situation (ie if I did try to close the thread cleany it would crash). otherwise I just return with a relevent error code.
ASKER
Answer accepted
when AfxEndThread is called it deallocates the objects on the stack - however It does not call thier destructors and CString does not deallocate the memory it allocated to store the string hence a leak