Thursday, May 29, 2014

Haunting memory leaks in Visual Studio with DEBUG_NEW

The simplest ever way to find where it leaks.

1. Insert well known piece of code in all .cpp files suspected for leakage:
#ifdef _DEBUG
#define new DEBUG_NEW
#endif
2. Use CMemoryState to dump blocks allocated within suspected region:
CMemoryState msOld;
msOld.Checkpoint();
CPerson* pper1 = new CPerson();
CPerson* pper2 = new CPerson();
msOld.DumpAllObjectsSince();
See further how #define DUMP_NEW can help you automate leak searching