How to enable Memory Leak Detection

7 03 2014

Memory leak is defined as the failure to correctly deallocate the memory that was previously allocated and it is a hard to detect bug in C++ applications. The program that leaks large amount of memory or leak progressively may decrease the performance of your system. These kinds of memory leaks make unexpected crashes in our applications and the most difficult part is to reproduce the defect and find out the root cause of same. So it is always better to analyze the memory leaks together with the functionality testing of our application. Lots of 3rd party tools are available for detecting memory leaks in our C++ code. AQTime is one of the best tools I ever used.But in this post I would like to discuss about enabling memory leak detection using Windows APIs.

The visual studio debugger along with C Run-Time Libraries (CRT) can help us to detect memory leaks in our code.

To enable the debug heap functions, include the following statements in your program:

#include "stdlib.h"
#include "crtdbg.h"

you can place a call to _CrtDumpMemoryLeaks before an application exit point to display a memory-leak report when your application exits:


This displays memory leak information in the Output window of Visual Studio IDE.

The #include statements must be in the order shown above. Otherwise, the functions you use may not work correctly.