CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 10 of 10
  1. #1
    Join Date
    Nov 2005
    Posts
    128

    Question Unknown memory leak.. Need help..

    My program is working pretty well however there is a memory that occurs and i don't know how to trace it. I've tried to delete the cWinThread object, terminate the thread. but still the leak is there..

    Here is some of the code:

    Quote Originally Posted by Code
    Under .h:
    CWinThread* cThread1;
    CWinThread* cThread2;

    static UINT StartThread1 (LPVOID param);
    static UINT StartThread2 (LPVOID param);

    Under .cpp:
    // Create thread begin execution

    void MainChildDlg2::OnStartButton()
    {
    cThread1 = AfxBeginThread(StartThread1, this );
    cThread2 = AfxBeginThread(StartThread2, this );
    }

    // Terminate thread
    void MainChildDlg2::OnStopButton()
    {
    DWORD lpExitCode1;
    DWORD lpExitCode2;

    // Retrieves termination status of thread
    GetExitCodeThread( cThread1->m_hThread, &lpExitCode1 );
    GetExitCodeThread( cThread2->m_hThread, &lpExitCode2 );

    hMutex = NULL;
    // Terminate thread

    if( TerminateThread( cThread1->m_hThread, 0 ) == FALSE )
    GetLastError();

    if( TerminateThread( cThread2->m_hThread, 0 ) == FALSE )
    GetLastError();

    cThread1 = NULL;
    cThread2 = NULL;
    CloseHandle( hMutex );

    m_cEditThread1.SetWindowText( "0" );
    m_cEditThread2.SetWindowText( "0" );

    delete cThread1;
    delete cThread2;
    }
    After the thread stops it doesn't show any memory leak but when I exit the program this is the message that appears:

    Detected memory leaks!
    Dumping objects ->
    thrdcore.cpp(166) : {140} client block at 0x00433140, subtype 0, 112
    bytes long.
    a CWinThread object at $00433140, 112 bytes long
    thrdcore.cpp(166) : {135} client block at 0x00433410, subtype 0, 112
    bytes long.
    a CWinThread object at $00433410, 112 bytes long
    Object dump complete.

    Thanks again.

  2. #2
    Join Date
    Dec 2005
    Posts
    642

    Re: Unknown memory leak.. Need help..

    Well, what do you expect?
    - you're setting cThread1, cThread2 to NULL before deleting them.
    - you're setting hMutex to NULL before closing it.
    - you're calling GetExitCodeThread before terminating the thread (with an exit code of 0).

    Apart from that, even if you solve all these problems, a thread killed by TerminateThread() will always cause a memory leak, although it may not show up on your leak detector.

  3. #3
    Join Date
    Dec 2002
    Location
    St.Louis MO, USA
    Posts
    672

    Re: Unknown memory leak.. Need help..

    TerminateThread is a dangerous function that should only be used in the most extreme cases. You should call TerminateThread only if you know exactly what the target thread is doing, and you control all of the code that the target thread could possibly be running at the time of the termination. For example, TerminateThread can result in the following problems:

    If the target thread owns a critical section, the critical section will not be released.
    If the target thread is executing certain kernel32 calls when it is terminated, the kernel32 state for the thread's process could be inconsistent.
    If the target thread is manipulating the global state of a shared DLL, the state of the DLL could be destroyed, affecting other users of the DLL.
    Also
    When Terminate Threadth is occur, the target thread has no chance to execute any user-mode code and its initial stack is not deallocated.
    So u have to use some flags in ur thread functions ( When that flags are true or false then u have to call AfxEndThred ). The Preffered way is, u have to use some Events and Wait functions to notify ur thread proc to exit. Take a look in MSDN Regarding syncronization, WaitForSingleObject , Events etc.
    Last edited by Naumaan; January 30th, 2006 at 06:42 AM.
    A Person who is polite is given goodness and a person who is away from Politeness is away from Goodness.

    NAUMAAN

  4. #4
    Join Date
    Nov 2005
    Posts
    128

    Re: Unknown memory leak.. Need help..

    Opss I forgot to change my code.. I'm using GetExitCodeThread inorder to retrieve the exit code for my two thread.

    This is what it should look like:

    Quote Originally Posted by GetExitCodeThread

    DWORD lpExitCode1;
    DWORD lpExitCode2;


    // Retrieves termination status of thread
    GetExitCodeThread( cThread1->m_hThread, &lpExitCode1 );
    GetExitCodeThread( cThread2->m_hThread, &lpExitCode2 );

    TerminateThread( cThread1->m_hThread, lpExitCode1 );
    TerminateThread( cThread2->m_hThread, lpExitCode2 );
    btw, I'm using a mutex for synchronization.

    Any advice how to get rid of memory leaks? Because the usual memory leaks I've encountered is that when you click the error, it would redirect you to where the leak is located in your program. But this leak, it redirects me to a "I think its a .cpp in the internal programming of MFC". Need Help guys...
    Thanks in advance..

  5. #5
    Join Date
    Nov 2005
    Posts
    128

    Re: Unknown memory leak.. Need help..

    ei, thanks guys.. thanks googler and naumaan.. I just comment out assigning of cthread1 and 2 to null before terminating and also with hmutex before closing it.
    Thanks again guys..

  6. #6
    Join Date
    Nov 2002
    Location
    California
    Posts
    4,556

    Re: Unknown memory leak.. Need help..

    But it's still dangerous to use TerminateThread. Don't do it, for reason that can be found almost everywhere, including the MSDN documentation page for this fucntion: http://msdn.microsoft.com/library/en...natethread.asp

    Read the part about TerminateThread being a "dangerous function"

    Mike

  7. #7
    Join Date
    Nov 2005
    Posts
    128

    Thumbs up Re: Unknown memory leak.. Need help..

    ei, thanks. but its ok because i'm using mutex instead of a critical section and that the thread I created has just a minimal operations. Not very dangerous operations. but thanks anyway..

  8. #8
    Join Date
    May 2000
    Location
    KY, USA
    Posts
    18,652

  9. #9
    Arjay's Avatar
    Arjay is offline Moderator / EX MS MVP Power Poster
    Join Date
    Aug 2004
    Posts
    13,490

    Re: Unknown memory leak.. Need help..

    Quote Originally Posted by mitz89
    ei, thanks. but its ok because i'm using mutex instead of a critical section and that the thread I created has just a minimal operations. Not very dangerous operations. but thanks anyway..
    Since you seem to be new to threading, I'll offer some unsolicited advice: Don't get in the habit of using terminate thread.

    Even though all the reasons listed in msdn that make TerminateThread a dangerous function don't seem to apply in this case (minimal operations, no cleanup, etc.), you would be better served to get in the practice of using an event to signal the thread to exit.

    That way, if the function becomes more complicated and requires cleanup (such as memory deletion or COM uninitialization), you will have already coded up a 'framework' that allows the thread to shut down properly.

    Arjay

  10. #10
    Join Date
    Nov 2005
    Posts
    128

    Re: Unknown memory leak.. Need help..

    Thanks guys for all your comments and advice..

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