Home > Software engineering >  VC2015 MFC multithreading memory leak problem
VC2015 MFC multithreading memory leak problem

Time:01-13

Kao VC2015 development program for a long time test, finally run out of memory, probably around 10 hours run die, created a thread in the program, while loop reads the images processed continuously,
According to the online posts, to monitor memory leaks, output information, modify some errors, have improved, but there are still leak,
Therefore, in a while to put the other code, just memory leak monitoring function, found that there are still leak,
Try to use AfxBeginThread CreateThread two different functions to create two different threads, comparison, all have leaked,
Below is according to the online posts, for memory leak monitoring several key operation:
1, in the dialog box of the thread start. H file to add code:

//memory leak check
 # ifdef _DEBUG 
# define _CRTDBG_MAP_ALLOC
# include
//# include & lt; The malloc. H>
# include
# endif


2, in the dialog box of the thread is started OnInitDialog () function, add the following code, to monitor memory leaks, and the results output to a file
 # ifdef _DEBUG 
Cstrings str_filename=_T (" memleak. Log ");
HANDLE hLogFile;//declaration log file handle
HLogFile=CreateFile (str_filename GENERIC_WRITE, FILE_SHARE_WRITE | FILE_SHARE_READ,
NULL, CREATE_ALWAYS FILE_ATTRIBUTE_NORMAL, NULL);//create a log file
_CrtSetReportMode (_CRT_WARN _CRTDBG_MODE_FILE);//of the content of the warn level output to a file (level of attention to dump report to warning)
_CrtSetReportFile (_CRT_WARN hLogFile);//sets the log file to the alarm output file
_CrtSetDbgFlag (_CrtSetDbgFlag (_CRTDBG_REPORT_FLAG) | _CRTDBG_ALLOC_MEM_DF | _CRTDBG_LEAK_CHECK_DF | _CRTDBG_CHECK_ALWAYS_DF);
# endif


3, a button in the dialog box, create and start a thread
 m_threadMassTest=AfxBeginThread (AFX_THREADPROC Grab2MassCheck, pDlgThis, THREAD_PRIORITY_HIGHEST); 


4, thread function
 UINT Grab2MassCheck (LPVOID pParam) 
{
While (TRUE)
{
SniffMemLeak (oldstate, newstate because diffstate);
}
return 0;
}

5, the custom memory leak monitoring function
 CMemoryState oldstate, newstate because diffstate; 

Void SniffMemLeak (CMemoryState & amp; Oldstate, CMemoryState & amp; Newstate because, CMemoryState & amp; Diffstate)
{
Newstate because the Checkpoint ();
If (diffstate. Difference (oldstate, newstate because))
{
Oldstate. DumpAllObjectsSince ();
Newstate because DumpAllObjectsSince ();
//dump between adjacent time point of the memory block change
Diffstate. DumpStatistics ();
}
Oldstate. Checkpoint ();

SYSTEMTIME * pnow_local_time=nullptr;
Pnow_local_time=new SYSTEMTIME;
GetLocalTime (pnow_local_time);
02 02 _RPT4 (_CRT_WARN, "% d % d: % 02 d: 02 % d: % 4 d snapshot dump. \ n \ n",
Pnow_local_time - & gt; WDay pnow_local_time - & gt; WHour pnow_local_time - & gt; WMinute pnow_local_time - & gt; WSecond pnow_local_time - & gt; WMilliseconds);
Free (pnow_local_time);
Pnow_local_time=nullptr;
}


6, start a thread, copy and copy of the text record memory leaks, open, can open to the inner leak, similar to the following:
.
 172} normal block at 0 x0000025242f94630, 40 bytes long. 
Data: & lt;> 04 00 00 00 00 00 00 00 D0 02 E8 14 00 00 FF 7 f
{171} normal block at 0 x0000025242f93460, 56 bytes long.
Data: & lt; 6 & gt; E8 36 D4 14 FF 7 f 00 00 04 00 00 0 CD CD CD CD
F: \ dd \ vctools \ vc7libs \ ship \ atlmfc \ SRC \ MFC \ oleinit CPP (84) : {167} client block at 0 x0000025242f6c740, subtype c0, 104 bytes long.
F: \ dd \ vctools \ vc7libs \ ship \ atlmfc \ SRC \ MFC \ dumpcont CPP (23) : atlTraceGeneral - a CCmdTarget object f6c740 at $0000025242, 104 bytes long
{83} client block at 0 x0000025242f8b090, subtype c0, 128 bytes long.
F: \ dd \ vctools \ vc7libs \ ship \ atlmfc \ SRC \ MFC \ dumpcont CPP (23) : atlTraceGeneral -a CDynLinkLibrary object f8b090 at $0000025242, 128 bytes long
The Object dump complete.
Dumping objects - & gt;
The Object dump complete.
0 bytes 0 in Free Blocks.
1372 bytes in 15 Normal Blocks.
31302 bytes in 143 CRT Blocks.
Zero bytes in 0 Ignore Blocks.
2120 bytes in 8 Client Blocks.
The Largest number 2:36644 bytes.
Total allocations: 314758 bytes.
12 10:50:29:0743 snapshot dump.

12 10:50:29:0743 snapshot dump.


Don't know how to solve this problem, hope expert advice, thank you very much!




  • Related