Detecting Memory Leaks

This article describes how to get the callstack for memory leaks in a
certain component (EXE or DLL) of your Visual C++ program.
The code provided with this article works on WindowsNT. It should work
on Windows9x systems as well, although it has not been tested on such
systems. There is another restriction, that it only works on Intel x86
machines. It has been written with MS Visual C/C++ 5.0, but it should
work fine with Version 6 of the Developer Studio as well.
The example program is written with the Active Template Library (ATL),
but it does not contain any COM stuff.

Usage

To detect memory leaks in the components of your application, you just
have to add the following lines anywhere to the code of your component:


	#include "c:tempHookAllocMemoryTracking.h"
	USE_MEMORYTRACKING("c:\temp\allocations.log",true)
	#pragma warning(disable:4073)
	#pragma init_seg(lib)
  • the path in the #include-statement has to be adapted to your needs
  • The Macro
    USE_MEMORYTRACKING(“c:\temp\allocations.log”, bLogAtStartup)
    defines the name of the Logfile (if NULL, all output is written
    to the debug output window) and specifies, whether logging is on
    or off at program startup

  • To control the parts of your program where logging is on / off
    use the macros LOGGING_ON / LOGGING_OFF or BEGIN_NO_LOG_BLOCK /
    END_NO_LOG_BLOCK to switch off logging for a special part of code
    within a single function/method.

  • The pragma you see above switches off the stupid compiler warning
    that an ‘initializer has been put in “lib” initialisation area’.
    This warning is not needed at all, since we know what we are doing
    with the next line: MemoryTracking is set up before any of the
    “users objects” (like MFC’s CWinApp derived object) are initialized.

Then:

  • recompile your component (rebuild is NOT neccessary)
  • start your program, and invoke those parts of it, where the leaks occur
  • terminate the program and you’ll get a list of memory leaks in your
    debug window of VC++

  • open the log file, that has been defined with the macro USE_MEMORYTRACKING
    (see above) or take a look into the VC++ debug output window, depending
    if you have defined a log filename or not

  • compare the numbers enclosed in curly braces (i.e. {1234} with the
    ‘request’-numbers in the log file (or in the debug output window)

By looking at the call stack in the logfile, you may figure out which
function or method has allocated the leak.

Tip:

If MFC is used, you may add the line ‘#define new DEBUG_NEW’ to the cpp
files of the project. Thus you provide information about where allocations
occur to the new operator. For allocations (leaks) that have been made
with DEBUG_NEW defined you can simply double-click the line preceeding the
call stack of a leak description (in the debug output window) to jump to
the appropriate position in the source code.

  • Please note that this mechanism only works in Debug mode and
    that it severly affects the execution speed of your program.
    This code has not been developed to be included permanently
    in your code, it should only be used to detect memory leaks.

  • Please not that the code can detect only those leaks that are
    caused in the component (DLL, OCX, EXE, …) where tracking is
    enabled. Tracking can only be enabled for one single component.

    Downloads

    Download demo project – 11 Kb

  • More by Author

    Must Read