The problem phenomenon is described as follows:
Detected memory leaks!
Dumping objects
F:\dd\vctools\vc7libs\ship\atlmfc\src\mfc\thrdcore.cpp (306): {271} client block at 0x0056b5f8, subtype C0, Bytes long .
Cause Analysis:
When you close a program and execute to a destructor, the child-threading function is forced to close at the end of the destructor. Because the child threads are forced to shut down, the child threads are closed after they are not executed.
As a result, the resources used by the child threads are not released in a timely manner, causing a memory leak.
Solution Ideas:
Since there is no normal end thread, then we have to let him end automatically should be OK, the following provides a reference scheme.
First step: Create global variables
bool G_bthread = TRUE; Child thread Loop condition
HANDLE G_heven t = NULL; Event Events
Step Two: create a no-signal event before creating the thread
G_hevent = CreateEvent (null,true,false,null);
Step Three: Set event event after the child thread loop is finished
while (G_bthread)
{
// ...
}
SetEvent (g_hevent);
Fourth Step: in the main program's destructor, modify the G_bthread and call WaitForSingleObject (G_hevent,infinite); Wait for the event state to change instead of shutting down the program directly. If the event state changes, it means that the child thread function jumps out of the loop and can exit normally.
G_bthread = FALSE;
WaitForSingleObject (G_hevent, INFINITE);
CloseHandle (g_hevent);
Note: This article references from http://blog.csdn.net/shuilan0066/article/details/6900986
About Thrdcore.cpp Memory leak issues