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.


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:\temp\HookAlloc\MemoryTracking.h"
	#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.

  • 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.


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.


    Download demo project - 11 Kb

  • Comments

    • There are no comments yet. Be the first to comment!

    Leave a Comment
    • Your email address will not be published. All fields are required.

    Top White Papers and Webcasts

    • IDC estimates that more than 70% of organizations have a cloud-first application deployment strategy to align with digital transformation initiatives. As such, many organizations are transitioning to Microsoft Office 365 and utilizing public clouds such as Microsoft Azure. However, IT leaders are discovering that default data protection schemes are not as robust as they desire. As a result, many are turning to Veeam® to provide the necessary data management and protection policies demanded by the …

    • Agencies that integrate back-office processed with cloud-based services, advanced analytics and talent management solutions can reduce costs, maximize efficiencies and transform employee recruitment and retention. Learn more by reading this interview brief by GovLoop with Infor industry thought leaders.

    Most Popular Programming Stories

    More for Developers

    RSS Feeds

    Thanks for your registration, follow us on our social networks to keep up-to-date