Memory Leak detection with Microsoft Visual C++ – Part 2

October 28th, 2009 Leave a Comment

I received some feedback from my previous post about the the memory leak detection. If the leaks originate from any files (in your project) other than the file containing the entry point, main() function, then the report will omit the file and line information.

So, how do we include a more detailed leak reporting for other files? You can add the following code in every cpp file after all the #include statements. See below for example.

#include <Windows.h>
#include <cassert>
// ...

// Start of snippet for memory leak reporting.
#ifdef _DEBUG
#include <crtdbg.h>
#define DEBUG_NEW new(_NORMAL_BLOCK, __FILE__, __LINE__)
#define new DEBUG_NEW
// End of snippet.

// ...
// void blahblah()

It is advisable that you add the snippet after all #include statements to avoid some weird errors caused some library headers. These errors may due to overriding of new operator, allocating memory using function ::operator new(), and etc. This will work exactly like the MFC’s leak reporting capability.

Hope it helps.

, ,

Leave a Reply

five − = 1