About Thrdcore.cpp Memory leak issues

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.