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

Thread: AfxBeginThread

Hybrid View

  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
    Hanover Germany
    Posts
    20,395

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

  4. #4
    Join Date
    Nov 2000
    Location
    Voronezh, Russia
    Posts
    6,620

    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

  5. #5
    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   

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

    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

  7. #7
    Join Date
    Apr 2000
    Location
    Belgium (Europe)
    Posts
    4,626

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

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

  9. #9
    2kaud's Avatar
    2kaud is offline Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    7,822

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

    C++23 Compiler: Microsoft VS2022 (17.6.5)

  10. #10
    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?

  11. #11
    Join Date
    Aug 2000
    Location
    New York, NY, USA
    Posts
    5,656

    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:
    FeinWindows - replacement windows manager for Visual Studio, and more...

  12. #12
    Join Date
    Apr 2000
    Location
    Belgium (Europe)
    Posts
    4,626

    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.

  13. #13
    Join Date
    Oct 2008
    Posts
    1,456

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

Posting Permissions

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





Click Here to Expand Forum to Full Width

Featured