CodeGuru
Earthweb Search
Forums Wireless Jars Gamelan Developer.com
CodeGuru Navigation
RSS Feeds

RSSAll

RSSVC++/C++

RSS.NET/C#

RSSVB

See more EarthWeb Network feeds

follow us on Twitter

Member Sign In
User ID:
Password:
Remember Me:
Forgot Password?
Not a member?
Click here for more information and to register.

Become a Marketplace Partner

jobs.internet.com

internet.commerce
Partners & Affiliates
















Home >> Visual C++ / C++ >> C++ >> C++ & MFC >> Memory Tracking


Detecting Memory Leaks
Rating:

Stefan Soltsien (view profile)
July 25, 1999

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


(continued)




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"
	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

    Tools:
    Add www.codeguru.com to your favorites
    Add www.codeguru.com to your browser search box
    IE 7 | Firefox 2.0 | Firefox 1.5.x
    Receive news via our XML/RSS feed







  • RATE THIS ARTICLE:   Excellent  Very Good  Average  Below Average  Poor  

    (You must be signed in to rank an article. Not a member? Click here to register)

    Latest Comments:
    Doesn't work - Legacy CodeGuru (12/18/2002)
    some help... - Legacy CodeGuru (11/13/2002)
    Doesn't catch 'COM-type" leaks - Legacy CodeGuru (04/30/2002)
    Why it is so slow? - Legacy CodeGuru (04/22/2002)
    A simpler way... - Legacy CodeGuru (04/16/2002)

    View All Comments
    Add a Comment:
    Title:
    Comment:
    Pre-Formatted: Check this if you want the text to display with the formatting as typed (good for source code)



    (You must be signed in to comment on an article. Not a member? Click here to register)