Click to See Complete Forum and Search --> : Unknown memory leak.. Need help..
mitz89
January 30th, 2006, 05:08 AM
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:
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. :)
googler
January 30th, 2006, 05:36 AM
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.
Naumaan
January 30th, 2006, 05:39 AM
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.
mitz89
January 30th, 2006, 05:50 PM
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:
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.. :)
mitz89
January 30th, 2006, 05:55 PM
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.. :)
MikeAThon
January 30th, 2006, 09:04 PM
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-us/dllproc/base/terminatethread.asp
Read the part about TerminateThread being a "dangerous function"
Mike
mitz89
January 30th, 2006, 11:01 PM
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.. :)
Andreas Masur
January 31st, 2006, 01:44 AM
Detecting and Isolating Memory Leaks (http://msdn.microsoft.com/library/default.asp?url=/library/en-us/vsdebug/html/vxcondetectingisolatingmemoryleaks.asp)
Detecting Memory Leaks in MFC (http://msdn.microsoft.com/library/default.asp?url=/library/en-us/vsdebug/html/_core_detecting_memory_leaks.asp)
Arjay
January 31st, 2006, 11:11 AM
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
mitz89
February 1st, 2006, 05:20 PM
Thanks guys for all your comments and advice.. :)
codeguru.com
Copyright Internet.com Inc., All Rights Reserved.