Sleeping in a thread causes a memory leak

I peer into my ancestor code and found a leak in the following situation:

1) Run the application
b) After starting the application, close the application within 4 seconds

Leakage Report:

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

Subsequently, I looked at the code, found a suspicious reason in 4 seconds. sleep while controlling the workflow function.

Testing 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 resolved.
However, I would like to know how this happens. Either because of the end of the workflow or the end of the GUI thread? Will the workflow terminate after the GUI thread causes this problem?

Can anyone revive my day by helping me explain this? I am lost...

+6
source share
3 answers

It seems that the workflow is not able to close properly after closing your application, as the process ends before it exits. The operating system, as a rule, is pretty well versed in resources by itself, so this may not be a problem. However, it is probably best if you wait for this thread to exit before allowing the application to close. Although it sounds like it will lead to a delay of 4 seconds when you turn off your application.

If this is unacceptable, you will need to add a mechanism to the thread to get the shutdown event from the main application thread. For example, if you replace the workflows with "sleep", with the WaitForSingleObject 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 you stop in your main thread, set the event, and 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 
+6
source

You must disconnect your threads gracefully before the process disappears. You can either start the main thread so that another thread exits, or send a signal to the main thread so that other threads exit.

+4
source

68 bytes?

If the application really closes, i.e. "Applications" and "Processes" disappeared from the task manager, and the only effect of this "leak" is the release of a debugging message at an early close, just release the debugging and forget about it.

This is likely to cancel the MFC shutdown, some structure that cannot be safely released during shutdown and left to the OS for cleaning.

When using 99.9% of applications that do not restart / stop, a 68-byte leak on shutdown, even if it has not been cleared, will not affect the operation of the Windows machine in any noticeable way between the reboot intervals every β€œpatch Tuesday” was forcibly executed .

I am sure that you have many more mistakes with more serious consequences. If not, you may have some of mine!

Rgds, Martin

0
source

Source: https://habr.com/ru/post/891414/


All Articles