开发者

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 0x016DF开发者_如何学运维A30, 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....


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


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.


68 bytes?

If the application does actually shut down, ie. has disappeared from the task manager 'Applications' and 'Processes', and the only effect of this 'leak' is to issue a debug message on early close, just turn the debug off and forget about it.

It's likely an abberation of MFC shutdown, some struct that cannot be safely freed during shutdown and is left around for the OS to clean up.

With the 99.9% of apps that are not continually restarted/stopped, a 68-byte leak on shutdown, even if it was not cleaned up, would not influence the operation of a Windows machine in any noticeable way between the reboot intervals enforced every 'Patch Tuesday'.

I'm sure you have plenty more bugs with more serious effects to deal with. If not, you can have some of mine!

Rgds, Martin

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜