Listening...
Main Topics
Browse All TopicsI'm experiencing a memory leak when running a thread. I've trimmed it down to the following code:
for (ii = 0; ii < 1000; ii++) {
CWinThread *pWinThread;
pWinThread = AfxBeginThread(ThreadLBPro
::Sleep(500);
}
UINT ThreadLBProc(LPVOID pParam)
{
return 0;
}
I know the 0.5 second sleep is crude, but I believe the thread terminates well within that time. I don't use this technique in the production code!
I appear to be losing about 160 bytes each time the thread is run, or about 160k after 1000 times around the loop. This figure of 160k is obtained from the 'Heap Usage' field in the Process Viewer utility.
I believe that the CWinThread, as I'm currently using it, should auto-delete when the thread terminates.
Any suggestions would be welcome!
This Question has been solved and asker verified All Experts Exchange premium technology solutions are available to subscription members.
Experts Exchange has been collecting answers to technology questions since 1996…3 million and counting! If you have a question, chances are we already have your answer.
If you can't find the exact answer you're looking for, ask our exclusive community of 50,000 experts. You’ll get a personalized answer from a trusted professional.
Thousands of free tech tips, tricks, how-to’s and tutorials are available in our peer reviewed articles section. See for yourself how smart our experts are, no login required.
Access the answers to your technology questions today.
30-day free trial. Register in 60 seconds.
Members of the expert community talk about why the experience at Experts Exchange is different than what you will find anywhere else.

Try it out and discover for yourself.
30-day free trial. Register in 60 seconds.
Join the community of experts here and help other tech pros by answering question in your area of expertise. You can earn FREE access to all Experts Exchange's premium features and resources.
jkr and Mahesh,
Thank you for your responses.
jkr - I tried your suggestion of adding a call to AfxEndThread() in the body of the thread procedure, and it made no difference: the same amount of heap was eaten. (Stepping through the code in THRDCORE.CPP, I can see that AfxEndThread() does get called as a result of the thread procedure terminating, without my calling it explicitly.)
Mahesh - the thread I'm starting is a worker thread, so has no message queue. My understanding is that CWinThread::InitInstance()
Regards,
Nick Willis
PS Here is the code fragment from THRDCORE.CPP:
UINT APIENTRY _AfxThreadEntry(void* pParam)
{
Thread started here
.
.
.
// else -- check for thread with message loop
else if (!pThread->InitInstance())
{
ASSERT_VALID(pThread);
nResult = pThread->ExitInstance();
}
.
.
.
// cleanup and shutdown the thread
threadWnd.Detach();
AfxEndThread(nResult);
return 0; // not reached
}
Hi William,
I tried this; the heap was initially 248k, had grown to 408k just before the loop ended, then dropped back to 384k.
Some more background:
My test program's CApp::InitInstance() method starts a 'control thread'. This control thread is the thread that runs the ThreadLBProc thread 1000 times.
If I just let the program run without the breakpoint (it starts a CMainWindow in CApp::InitInstance() so the program doesn't terminate until I kill the window) then I get the same result.
Regards,
Nick
Hi Nick
How abt using the ExitThread function to see exiting the thread forcefully does not consume memory.i mean no part of memory is used even at the end of thread.
For getting the code of the currently running thread there is an API function called GetExitCodeThread which will return the code for the thread theat needs to be terminated.
Then call the global function
VOID ExitThread(
DWORD dwExitCode // exit code for this thread
);
here dwExitCode=GetExitCodeThre
Regards
Mahesh Sundararaman
Mahesh, thanks for your suggestion, but I feel that a normal thread termination should clean up its memory tidily (and I have stepped through the code in THRDCORE.CPP which appears to do just that) so I don't think that forcing a thread termination would help.
I'm basing my opinion that there *is* a leak, on the 'Heap usage' field in the process viewer utility, by clicking repeatedly on the 'Refresh' button and seeing the value rise periodically. Am I just misinterpreting the symptoms?
Dear hirop
From the website www.numega.com download the evaluation copy of BoundsChecker.It will analyse ur source code and exactly specifies where does memory leak occur.
Regards
Mahesh Sundararaman
This question didn't show any activity for more than 21 days. I will ask Community Support to close it unless you finalize it yourself within 7 days.
You can always request to keep this question open. But remember, experts can only help if you provide feedback to their comments.
Unless there is objection or further activity, I will suggest to
"refund the points and PAQ at zero points"
PLEASE DO NOT ACCEPT THIS COMMENT AS AN ANSWER!
========
Werner
No comment has been added lately, so it's time to clean up this TA.
I will leave a recommendation in the Cleanup topic area that this question is:
PAQ'd with the points forfeited
Please leave any comments here within the next seven days.
PLEASE DO NOT ACCEPT THIS COMMENT AS AN ANSWER!
martynjpearson
EE Cleanup Volunteer
Business Accounts
Answer for Membership
by: jkrPosted on 2001-02-27 at 10:46:07ID: 5884298
The docs are pretty unprecise on that. Do you notice the same behaviour when using
UINT ThreadLBProc(LPVOID pParam)
{
AfxEndThread ( 0);
return 0;
}