Click here to Skip to main content
13,450,606 members (57,480 online)
Click here to Skip to main content
Add your own
alternative version


48 bookmarked
Posted 11 Sep 2000

Catching memory leaks

, 4 Oct 2000
Rate this:
Please Sign up or sign in to vote.
How catch memory leaks with very little effort
<!-- Link to demo file download -->
  • Download demo project - 7 Kb
  • <!-- Add the rest of your HTML here -->


    I would like share with you some experience in catching memory leaks. In most case it is a long processe and required additional tools like PurifyNT or BoundsChecker. Actually, catching simple memory leaks is possible by using the Microsoft exported functionality.

    In a simple case you should add several lines to 'StdAfx.h'

    #ifdef _DEBUG
       #define _CRTDBG_MAP_ALLOC // include Microsoft memory leak detection procedures
       #define _INC_MALLOC	     // exclude standard memory alloc procedures

    Note: it should be before the 'include' directives.

    In the enclosed example you will find an example with a memory leak. To see how it works you should run program under the debugger. When the program finishes you will see in the the 'Output' window, 'Debug' tab the following message.

    Detected memory leaks!
    Dumping objects -> D:\Projects\CrtDbg\CrtDbg.cpp(25) : {53} normal block at 0x002F26C0, 10 bytes long.
    Data: <MemoryLeak> <MEMORYLEAK> 4D 65 6D 6F 72 79 4C 65 61 6B 
    Object dump complete.

    In more advanced memory management cases you should look in the 'AfxMem.cpp' file in MFC source directory. File contents plenty of memory management functions.

    I have written a class CMemDiff that wraps CMemoryState and helps track memory leaks. Just include the MemDiff files in your project. A global variable of type CMemDiff is declared and it's constructor and destructor will check the state of your memory at the start and end of your program and report any leaks.


    This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

    A list of licenses authors might use can be found here


    About the Author

    Audrius Vasiliauskas
    Technical Lead Doclogix
    Lithuania Lithuania
    No Biography provided

    You may also be interested in...

    Comments and Discussions

    GeneralUnhandled exception in ntdll Pin
    ed welch16-Apr-09 12:59
    membered welch16-Apr-09 12:59 
    GeneralRe: Unhandled exception in ntdll Pin
    Audrius Vasiliauskas18-Apr-09 6:53
    memberAudrius Vasiliauskas18-Apr-09 6:53 

    General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

    Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

    Permalink | Advertise | Privacy | Terms of Use | Mobile
    Web04 | 2.8.180318.3 | Last Updated 5 Oct 2000
    Article Copyright 2000 by Audrius Vasiliauskas
    Everything else Copyright © CodeProject, 1999-2018
    Layout: fixed | fluid