AfxBeginThread
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Page 1 of 2 12 LastLast
Results 1 to 15 of 16

Thread: AfxBeginThread

  1. #1
    Join Date
    Jul 2013
    Posts
    6

    AfxBeginThread

    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);
    }

    In some part of the main dlg body:

    UINT CTestThreadDlg::testThread(LPVOID pParam)
    {
    return 0;
    }

    I also tried doing the following without any luck.
    In OnInitDialog():

    CWinThread *m_Thread=NULL;

    for (;{
    if (m_Thread) {
    delete m_Thread;
    m_Thread=NULL;
    }
    m_Thread=AfxBeginThread(testThread, this, 0, 0, CREATE_SUSPENDED, NULL);
    m_Thread->m_bAutoDelete=FALSE;
    m_Thread->ResumeThread();
    WaitForSingleObject(m_Thread->m_hThread, INFINITE);
    }

    Can anybody please explain why its happening and what can be done to make it right?
    Thanks in advance.

  2. #2
    VictorN's Avatar
    VictorN is offline Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Wallisellen (ZH), Switzerland
    Posts
    17,517

    Re: AfxBeginThread

    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
    Victor Nijegorodov

  3. #3
    Join Date
    Apr 2000
    Location
    Belgium (Europe)
    Posts
    4,049

    Re: AfxBeginThread

    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).

  4. #4
    Join Date
    Jul 2013
    Posts
    6

    Re: AfxBeginThread

    Thank you for your comment. But my main thread never exits. The threads are created from OnInitDialog().


    Quote Originally Posted by OReubens View Post
    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).

  5. #5
    Join Date
    Jul 2013
    Posts
    6

    Re: AfxBeginThread

    Thank you for your reply. I have attached the project. Running it in debug mode I found 200KB mem increase in 20mins.

    Quote Originally Posted by VictorN View Post
    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
    Attached Files Attached Files

  6. #6
    Join Date
    Dec 2012
    Location
    England
    Posts
    2,711

    Re: AfxBeginThread

    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. You are ultimately responsible for effects of your programs and the integrity of the machines they run on.

  7. #7
    Join Date
    Jul 2013
    Posts
    6

    Re: AfxBeginThread

    Stay constant.

    Quote Originally Posted by 2kaud View Post
    And if you comment out the AfxBeginThread line does the memory usage stay constant or still increase?

  8. #8
    Join Date
    Nov 2000
    Location
    Voronezh, Russia
    Posts
    6,039

    Re: AfxBeginThread

    Quote Originally Posted by carbuet View Post
    I have attached the project. Running it in debug mode I found 200KB mem increase in 20mins.
    Nope. Being built with VS2010 and run, release or debug, whatever, it shows stable figures in Task Manager.
    Best regards,
    Igor

  9. #9
    Join Date
    Aug 2000
    Location
    New York, NY, USA
    Posts
    5,545

    Re: AfxBeginThread

    Quote Originally Posted by carbuet View Post
    Stay constant.
    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:
    FeinViewer - an integrated GDI objects viewer for Visual C++ Debugger, and more...

  10. #10
    Join Date
    Jul 2013
    Posts
    6

    Re: AfxBeginThread

    I tried with VS2010. After running it for 4hrs it show 400KB increase. Pls see the attached snapshots.

    Quote Originally Posted by Igor Vartanov View Post
    Nope. Being built with VS2010 and run, release or debug, whatever, it shows stable figures in Task Manager.
    Attached Images Attached Images   

  11. #11
    Join Date
    Nov 2000
    Location
    Voronezh, Russia
    Posts
    6,039

    Re: AfxBeginThread

    Quote Originally Posted by carbuet View Post
    I tried with VS2010. After running it for 4hrs it show 400KB increase. Pls see the attached snapshots.
    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?
    Best regards,
    Igor

  12. #12
    Join Date
    Jul 2013
    Posts
    6

    Re: AfxBeginThread

    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.

    Quote Originally Posted by Igor Vartanov View Post
    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?

  13. #13
    Join Date
    Apr 2000
    Location
    Belgium (Europe)
    Posts
    4,049

    Re: AfxBeginThread

    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.

  14. #14
    Join Date
    Oct 2008
    Posts
    1,162

    Re: AfxBeginThread

    to avoid the effects of continuously creating/destroying thread objects, you could modify your second code snippet like this:

    Code:
    CWinThread *m_Thread=NULL;
    
    for(;;)
    {
    	if (m_Thread) {
    		m_Thread->CreateThread( CREATE_SUSPENDED );
    	}
    	else {
    		m_Thread=AfxBeginThread(testThread, this, 0, 0, CREATE_SUSPENDED, NULL);
    	}
    	m_Thread->m_bAutoDelete=FALSE;
    	m_Thread->ResumeThread();
    	WaitForSingleObject(m_Thread->m_hThread, INFINITE);
    }
    of course, there's no guarantee this will actually give you "perfectly constant" numbers in taskmanager ...

  15. #15
    Join Date
    Nov 2000
    Location
    Voronezh, Russia
    Posts
    6,039

    Re: AfxBeginThread

    Quote Originally Posted by carbuet View Post
    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.
    Okay, here it goes, 4 hrs log.
    mon.zip

    The figure you were looking at was Working Set. The figure I was looking at was Virtual Memory (a.k.a. Private Working Set). See the difference.
    Best regards,
    Igor

Page 1 of 2 12 LastLast

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center