This documentation is archived and is not being maintained.

Enabling Memory Leak Detection

The primary tools for detecting memory leaks are the debugger and the CRT debug heap functions. To enable the debug heap functions, include the following statements in your program:

#define CRTDBG_MAP_ALLOC
#include <stdlib.h>
#include <crtdbg.h>
Note   The #include statements must be in the order shown here. If you change the order, the functions you will use may not work properly.

By including crtdbg.h, you map the malloc and free functions to their Debug versions, _malloc_dbg and _free_dbg, which keep track of memory allocation and deallocation. This mapping occurs only in a debug build (in which _DEBUG is defined). Release builds use the ordinary malloc and free functions.

The #define statement maps the base versions of the CRT heap functions to the corresponding Debug versions. You do not absolutely need this statement, but without it, the memory leak dump will contain less useful information.

Once you have added the statements shown above, you can dump memory leak information by including the following statement in your program:

_CrtDumpMemoryLeaks();

When you run your program under the debugger, _CrtDumpMemoryLeaks displays memory leak information in the Output window. The memory leak information looks like this:

Detected memory leaks!
Dumping objects ->
C:\PROGRAM FILES\VISUAL STUDIO\MyProjects\leaktest\leaktest.cpp(20) : {18} 
normal block at 0x00780E80, 64 bytes long.
 Data: <                > CD CD CD CD CD CD CD CD CD CD CD CD CD CD CD CD
Object dump complete.

If you do not use the #define _CRTDBG_MAP_ALLOC statement, the memory leak dump would look like this:

Detected memory leaks!
Dumping objects ->
{18} normal block at 0x00780E80, 64 bytes long.
 Data: <                > CD CD CD CD CD CD CD CD CD CD CD CD CD CD CD CD
Object dump complete.

Without _CRTDBG_MAP_ALLOC defined, the display shows:

  • The memory allocation number (inside the curly braces).
  • The block type (normal, client, or CRT).
  • The memory location in hexadecimal form.
  • The size of the block in bytes.
  • The contents of the first 16 bytes (also in hexadecimal).

With _CRTDBG_MAP_ALLOC defined, the display also shows you the file where the leaked memory was allocated. The number in parentheses following the filename (20, in this example) is the line number within the file.

To go to the line in the source file where the memory is allocated

  • Double-click on the line in the Output window that contains the filename and line number.

    -or-

  • Select the line in the Output window that contains the filename and line number and press F4.

_CrtSetDbgFlag

Calling _CrtDumpMemoryLeaks is easy enough if your program always exits in the same place, but what if your program can exit from multiple locations? Instead of putting a call to _CrtDumpMemoryLeaks at each possible exit, you can include the following call at the beginning of your program:

_CrtSetDbgFlag ( _CRTDBG_ALLOC_MEM_DF | _CRTDBG_LEAK_CHECK_DF );

This statement automatically calls _CrtDumpMemoryLeaks when your program exits. You must set both bit fields, _CRTDBG_ALLOC_MEM_DF and _CRTDBG_LEAK_CHECK_DF, as shown above.

Setting the CRT Report Mode

By default, _CrtDumpMemoryLeaks dumps memory leak information to the Debug pane of the Output window, as described above. You can reset this to dump to another location using _CrtSetReportMode. If you use a library, it may reset the output to another location. In that case, you can set the output location back to the Output window using the following statement:

_CrtSetReportMode( _CRT_ERROR, _CRTDBG_MODE_DEBUG );

For information on using _CrtSetReportMode to send output to other locations, see _CrtSetReportMode.

See Also

Detecting and Isolating Memory Leaks

Show: