Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

A sleep in thread causes memory leak

I peek into my ancestor's code and found out a leak in the following situation:

1) Launch application
b) After application is launched, close the applicaiton within 4 secs

The leak message:

f:\dd\vctools\vc7libs\ship\atlmfc\src\mfc\thrdcore.cpp(306) : {58509} client block at 0x016DFA30, subtype c0, 68 bytes long.

Subsequently, I went through the code, found out the suspicious point of cause at a 4secs of sleep at controlling function of worker thread.

The test program:

UINT InitThread(LPVOID pParam)
{
      Sleep(4000);  //4000 is the default value, it reads from a registry key.
      CMyMFCTestProjectDlg* pTest = (CMyMFCTestProjectDlg*)pParam;
      pTest->DoSomething();  
      return 0;  //--> Exit thread
}

BOOL CMyMFCTestProjectDlg::OnInitDialog() {
...
AfxBeginThread(InitThread, this);
...
}

If I reduce/remove the sleep timer, the leak will be solved.
However, I would like to know how does it happen. Either due to worker thread or GUI thread termination? Will worker thread exits after GUI thread will cause this problem?

Anyone can cheer up my day by helping me to explain this? I'm lost....

like image 568
wengseng Avatar asked Jun 27 '11 03:06

wengseng


People also ask

Can threads cause memory leaks?

A thread leak is causing a memory shortage at the server, which will cause the JVM process to throw out an OOM error.

What is the main cause of memory leaks?

DEFINITION A memory leak is the gradual deterioration of system performance that occurs over time as the result of the fragmentation of a computer's RAM due to poorly designed or programmed applications that fail to free up memory segments when they are no longer needed.

What happens when a sleep is called on thread?

sleep() functions to execute, it always pauses the current thread execution. If any other thread interrupts when the thread is sleeping, then InterruptedException will be thrown.

Why should we not use thread sleep?

Thread. sleep is bad! It blocks the current thread and renders it unusable for further work.


2 Answers

It sounds like the worker thread is not given a chance to close itself properly after your app closes, since the process ends before it exits. The operating system is usually pretty good at cleaning up resources on its own, so it may not be a problem. However, it's probably best if you wait for that thread to exit before allowing the application to shut down. Though it sounds like that that will cause a 4 second delay in the shutdown of your app.

If that's unacceptable, you will have to add some mechanism to the thread, to receive the shutdown event from the apps main thread. For example, if you replace the worker threads "sleep", with a WaitForSingleObject of an event:

DWORD res = WaitForSingleObject(
    shutdownEvent,
    4000); // timeout
if(res == WAIT_OBJECT_0)
{
    // received the shutdownEvent, exit
    return 0;
}
// The delay has elapsed, continue with rest of thread.
. . .

Then, when your are shutting down in your main thread, set the event, then wait for the thread to exit, it should exit almost immediately:

SetEvent(this->shutdownEvent);
WaitForSingleObject(pThread->m_hThread, INFINITE); // pThread is returned from AfxBeginThread
like image 154
zdan Avatar answered Oct 13 '22 15:10

zdan


You should shutdown your threads gracefully before your process goes away. You can either have the main thread wait for the other thread(s) to exit or have the main thread signal the other thread(s) to exit.

like image 31
travis0xFF Avatar answered Oct 13 '22 13:10

travis0xFF