Click here to Skip to main content
Licence 
First Posted 11 Sep 2000
Views 172,876
Bookmarked 47 times

Catching memory leaks

By | 4 Oct 2000 | Article
How catch memory leaks with very little effort
  • Download demo project - 7 Kb
  • Introduction

    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
    #endif
    

    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.

    License

    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

    Business Analyst

    Lithuania Lithuania

    Member



    Sign Up to vote   Poor Excellent
    Add a reason or comment to your vote: x
    Votes of 3 or less require a comment

    Comments and Discussions

     
    You must Sign In to use this message board. (secure sign-in)
     
    Search this forum  
     FAQ
        Noise  Layout  Per page   
      Refresh
    GeneralUnhandled exception in ntdll Pinmembered welch12:59 16 Apr '09  
    GeneralRe: Unhandled exception in ntdll PinmemberAudrius Vasiliauskas6:53 18 Apr '09  
    Generalerror C2065: 'alloca' : undeclared identifier Pinmembersafety_ruk22:13 25 May '06  
    GeneralRe: error C2065: 'alloca' : undeclared identifier Pinmemberakulkarni11:42 19 Jul '06  
    GeneralRe: error C2065: 'alloca' : undeclared identifier Pinmemberakulkarni11:42 19 Jul '06  
    Questionafxpriv.h Pinmemberehh8:49 8 Sep '05  
    QuestionHow to find real leaked point Pinmembermurakami22:44 15 Aug '05  
    AnswerRe: How to find real leaked point PinmemberTim Read6:23 29 May '06  
    AnswerRe: How to find real leaked point Pinmembermurakami17:01 29 May '06  
    GeneralRe: How to find real leaked point PinmemberTim Read23:20 30 May '06  
    GeneralDebugging a DLL - no StdAfx.h available Pinmemberbschorre2:00 30 Mar '05  
    GeneralAn extremely useful tip PinsussAnonymous19:52 11 Apr '04  
    GeneralRe: An extremely useful tip Pinmemberworksopbenny22:54 7 Jun '04  
    GeneralSoooooooo Coooooool Pinmemberinternal16:28 10 Jun '04  
    GeneralRe: An extremely useful tip Pinmemberpeterboulton1:24 9 Dec '04  
    GeneralRe: An extremely useful tip PinmemberRainKing Fool15:43 4 Oct '05  
    Generalabout VC7 PinmemberMuhammad Ahmed0:14 5 Feb '04  
    GeneralNot Getting the path where actully memory leak occuring PinmemberJaymin2:26 5 Aug '03  
    GeneralMemory Leak PinsussSivakumar R23:59 27 Mar '03  
    Generalabout call stack PinmemberNing Cao15:55 7 Jan '03  
    GeneralRe: about call stack PinmemberMike Nordell16:25 7 Jan '03  
    GeneralRe: about call stack PinmemberNing Cao16:35 7 Jan '03  
    GeneralRe: about call stack PinmemberKita21:33 24 Jun '03  
    Generalabout custom memory management PinmemberNing Cao15:50 7 Jan '03  
    GeneralMemory leak detection PinmemberYud21:48 27 Feb '01  

    General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    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 | Mobile
    Web01 | 2.5.120517.1 | Last Updated 5 Oct 2000
    Article Copyright 2000 by Audrius Vasiliauskas
    Everything else Copyright © CodeProject, 1999-2012
    Terms of Use
    Layout: fixed | fluid