Memory Leaks In VC|
I recently found out that this little trick to detect memory leaks in VC isn't
that well known to coders all around, and since FlipCode has been an inspiration
and a source of information for me for so long time, I thought it would be time
for me to contribute with a little tip of the day.
How to detect memory leaks in Visual C++:
To enable memory leak detection in VC put the following in your code (winmain
int flag = _CrtSetDbgFlag(_CRTDBG_REPORT_FLAG); // Get current flag
flag |= _CRTDBG_LEAK_CHECK_DF; // Turn on leak-checking bit
_CrtSetDbgFlag(flag); // Set flag to the new value
I've been using this trick quite a while to detect memory leaks directly in VC.
It works for debugbuilds executed in debugmode (F5) and it will report any leaks
to the output window when you exit the application. The output isn't very
usefull, but it can be used to detect if there are any leaks at all.
It is my experience that You have to use the leak detection from the beginning
of a project and continue to keep the leaks to a zero throughout the progression
of the project since this is your only chance to narrow down where the leaks are
located whenever a new one occur. If You try to use the leak detection on older
projects You will probably find several 100's of leaks which IMHO is nearly
impossible to narrow down and fix due to the bad quality of the output.
The zip file viewer built into the Developer Toolbox made use
of the zlib library, as well as the zlibdll source additions.