简体   繁体   中英

Visual C++, MFC Application, No Malloc, etc at all: What can cause Memory Leak?

Beginner to C++.

I've spent 8+ hours trying to find Google help here.

(added: I installed the trial version of PurifyPlus but my application does not run "inside" it for some reason).

I have an MFC application with a persistent memory leak.

There are a series of threads as: DWORD WINAPI WorkThread( LPVOID lParam ) each of which calls threads as: void TradeLogic( CIBTraderDlg *pDlg ) on a timer.

There is a dialog featuring ListView controls.

There are no "malloc" or "calloc" calls anywhere in the application. (search includes full API) There is a single "new" socket that executes only once when the program starts. There are no dynamic allocations with "new." There is a single read from a file that occurs only once, when the program starts.

But there is a memory leak that persists, ad infinitum. (Roughly 4K bytes every 15 seconds, not exactly uniform though)

My program does not fully run in debug mode. The dialog comes up, but it does not execute any of the calls to a remote server's time thus not the events that key off this (including updating listviews).

Nonetheless, running in "Debug" so qualified, when I click my "Exit" button the Debug output shows the CRT messages as in the documentation but they are not overtly helpful as no leak has occurred at all (they suggest the "new" for the socket can leak but, again, the socket is opened only once). Taskmgr confirms no leak is occurring in this limited mode.

So, I followed the MSDN and tried the following code in the "Exit" sequence:

HANDLE hLogFile;
hLogFile = CreateFile("c:\\log.txt", GENERIC_WRITE, FILE_SHARE_WRITE, NULL, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);
_CrtSetReportMode(_CRT_ERROR, _CRTDBG_MODE_FILE);
_CrtSetReportFile(_CRT_ERROR, hLogFile);

_RPT0(_CRT_ERROR,"file message\n");
_CrtDumpMemoryLeaks();
CloseHandle(hLogFile);
exit(0);

and ran the program normally. But when I click Exit I only get the text "file message" with none of the output of CRT at all to examine.

Can someone point me to where I might look in the code for problems?

You can try this library to detect leaks easily, but I'm guessing the problem is that Windows is allocating memory, so this won't help.

https://vld.codeplex.com/

Next, test your app with only a single worker thread to make sure you don't have race conditions. If it works with only a single worker thread, then you have multithreading issues.

If testing with a single thread doesn't help, then a straightforward way to debug the problem is to comment out a section of the application (such as the function calls that do the processing), run the app, see if it still leaks, and repeat until you narrow down the problem. I'd start by commenting out all interaction with the ListView.

If you are trying to update the ListView from multiple threads, then you are going to have trouble. Windows controls like a ListView only run in the primary thread, so all worker threads will block that are updating the ListView.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM