Something I am not being able to understand. I am using VC2005. In MFC I am creating threads repeatedly. The thread is basically doing nothing. Its an empty block. So, there should not be any memory leak. But I am seeing constant memory increase in the task manager. Here is my code.
In OnInitDialog():
for (;{
AfxBeginThread(testThread, this);
Sleep(50);
}
Could you post a very small test project that can reproduce such a behaviour with memory leaks?
Please, pack it in zip archive and do not include debug/release folders nor .asp, .ncb, .opt files
if your main thread ends before all the created threads have completed, then you'll have a memory leak because all the child threads will be terminated (without allowing them proper cleanup) by the system.
a simple proof for this would be to add a 'long enough' Sleep() before your main thread ends. But this isn't a correct way to solve the actual problem.
If you create a thread, then you should have some form of synchronisation in your main thread that guarantees that ALL the worker threads have finished, before it exits to the system.
Note that if you call AfxBeginThread, then MFC will allocate a CWinThread object, if the thread isn't allowed to cleanup (or you don't do it in lieu of the normal thread cleanup), then you'll be leaking that object (at the least, potentially more).
Thank you for your comment. But my main thread never exits. The threads are created from OnInitDialog().
Originally Posted by OReubens
if your main thread ends before all the created threads have completed, then you'll have a memory leak because all the child threads will be terminated (without allowing them proper cleanup) by the system.
a simple proof for this would be to add a 'long enough' Sleep() before your main thread ends. But this isn't a correct way to solve the actual problem.
If you create a thread, then you should have some form of synchronisation in your main thread that guarantees that ALL the worker threads have finished, before it exits to the system.
Note that if you call AfxBeginThread, then MFC will allocate a CWinThread object, if the thread isn't allowed to cleanup (or you don't do it in lieu of the normal thread cleanup), then you'll be leaking that object (at the least, potentially more).
Thank you for your reply. I have attached the project. Running it in debug mode I found 200KB mem increase in 20mins.
Originally Posted by VictorN
Could you post a very small test project that can reproduce such a behaviour with memory leaks?
Please, pack it in zip archive and do not include debug/release folders nor .asp, .ncb, .opt files
And if you comment out the AfxBeginThread line does the memory usage stay constant or still increase?
All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!
That only proves that repeated calls to Sleep() do not leak. Who would doubt that?
Your code basically creates a new thread every 50 ms. On a very slow (or very busy) system, if the thread creation and tear down take more than 50 ms, the number of running threads (with whatever memory is allocated for them) will increase.
Could you add Thread count column to your task manager? Does it increase with time?
Vlad - MS MVP [2007 - 2012] - www.FeinSoftware.com
Convenience and productivity tools for Microsoft Visual Studio: FeinWindows - replacement windows manager for Visual Studio, and more...
Will definitely prove that the program is behaving differently in different machines, possibly different OS/SP. The question is WHY? I compiled it in XP with VS2005, ran it in WIN7. The result is the same.
Originally Posted by Igor Vartanov
And will that prove anything in case I provide screenshots of mine?
Did you try that on a clean machine? Some virtual one for beginning?
I'm going to repeat myself here...
each time you create a thread with AfxbeginThread, MFC allocates a CWinThread,
(the OS itself will allocate stack space for each thread)
you don't provide any synchronisation, so multiple threads may end up being active at the same time before they close and cleanup.
while the OS will cleanup the thread stack, the heap is not typically shrunk.
this means you'll be increasing the heap size of your program, and this memory isn't recovered until you exit.
Will definitely prove that the program is behaving differently in different machines, possibly different OS/SP. The question is WHY? I compiled it in XP with VS2005, ran it in WIN7. The result is the same.
* The Best Reasons to Target Windows 8
Learn some of the best reasons why you should seriously consider bringing your Android mobile development expertise to bear on the Windows 8 platform.