Link to home
Create AccountLog in
Avatar of mfc_speak
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?
Avatar of ShaunWilde
ShaunWilde

I believe this is the reason

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
ASKER CERTIFIED SOLUTION
Avatar of ShaunWilde
ShaunWilde

Link to home
membership
Create a free account to see this answer
Signing up is free and takes 30 seconds. No credit card required.
See answer
Avatar of mfc_speak

ASKER

Hi,
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.
Answer accepted