Showing posts with label memory leaks. Show all posts
Showing posts with label memory leaks. Show all posts

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

Friday, August 9, 2013

A tool for searching for memory leaks



1. Download and install utility from http://vld.codeplex.com  (it's free).
2. In VS invoke dialog Tools -> Options..., select page Projects and Solutions -> VC++ Directories and add 
C:\Program Files (x86)\Visual Leak Detector\include 
to the list of Include files, and 
C:\Program Files (x86)\Visual Leak Detector\lib\Win32
to Library files.
3. Restart Visual Studio.
4. In any project file, add line 
#include "vld.h".
5. Build the project and run from the debugger.
6. After exiting the program, Output window will contain additional information about some (but unfortunately not all) of memory leaks, which makes it easy to locate them.
7. Please don't forget to remove #include "vld.h" before committing :)

by Oleg.B