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?
System Programming

Avatar of undefined
Last Comment
mfc_speak
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

Blurred text
THIS SOLUTION IS ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
Avatar of mfc_speak
mfc_speak

ASKER

Hi,
Should I use AfxEndThread(1) or return 1 to exit a thread prematurely?
Avatar of ShaunWilde
ShaunWilde

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.
Avatar of mfc_speak
mfc_speak

ASKER

Answer accepted
System Programming
System Programming

Kernel and system programming is the process of creating the software necessary for a computer or device to function and operate other programs. Some operating systems (such as Microsoft Windows) are proprietary, but others, such as the various Linux distributions, are open source.

41K
Questions
--
Followers
--
Top Experts
Get a personalized solution from industry experts
Ask the experts
Read over 600 more reviews

TRUSTED BY

IBM logoIntel logoMicrosoft logoUbisoft logoSAP logo
Qualcomm logoCitrix Systems logoWorkday logoErnst & Young logo
High performer badgeUsers love us badge
LinkedIn logoFacebook logoX logoInstagram logoTikTok logoYouTube logo