Click here to Skip to main content
Click here to Skip to main content

Catching memory leaks

By , 4 Oct 2000
 
  • 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
    No Biography provided

    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

     
    Hint: For improved responsiveness ensure Javascript is enabled and choose 'Normal' from the Layout dropdown and hit 'Update'.
    You must Sign In to use this message board.
    Search this forum  
        Spacing  Noise  Layout  Per page   
    GeneralUnhandled exception in ntdllmembered welch16 Apr '09 - 12:59 
    GeneralRe: Unhandled exception in ntdllmemberAudrius Vasiliauskas18 Apr '09 - 6:53 
    Generalerror C2065: 'alloca' : undeclared identifiermembersafety_ruk25 May '06 - 22:13 
    GeneralRe: error C2065: 'alloca' : undeclared identifiermemberakulkarni19 Jul '06 - 11:42 
    GeneralRe: error C2065: 'alloca' : undeclared identifiermemberakulkarni19 Jul '06 - 11:42 
    Questionafxpriv.hmemberehh8 Sep '05 - 8:49 
    QuestionHow to find real leaked pointmembermurakami15 Aug '05 - 22:44 
    AnswerRe: How to find real leaked pointmemberTim Read29 May '06 - 6:23 
    AnswerRe: How to find real leaked pointmembermurakami29 May '06 - 17:01 
    GeneralRe: How to find real leaked pointmemberTim Read30 May '06 - 23:20 
    GeneralDebugging a DLL - no StdAfx.h availablememberbschorre30 Mar '05 - 2:00 
    GeneralAn extremely useful tipsussAnonymous11 Apr '04 - 19:52 
    GeneralRe: An extremely useful tipmemberworksopbenny7 Jun '04 - 22:54 
    GeneralSoooooooo Cooooooolmemberinternal10 Jun '04 - 16:28 
    GeneralRe: An extremely useful tipmemberpeterboulton9 Dec '04 - 1:24 
    GeneralRe: An extremely useful tipmemberRainKing Fool4 Oct '05 - 15:43 
    Generalabout VC7memberMuhammad Ahmed5 Feb '04 - 0:14 
    GeneralNot Getting the path where actully memory leak occuringmemberJaymin5 Aug '03 - 2:26 
    GeneralMemory LeaksussSivakumar R27 Mar '03 - 23:59 
    Generalabout call stackmemberNing Cao7 Jan '03 - 15:55 
    GeneralRe: about call stackmemberMike Nordell7 Jan '03 - 16:25 
    GeneralRe: about call stackmemberNing Cao7 Jan '03 - 16:35 
    GeneralRe: about call stackmemberKita24 Jun '03 - 21:33 
    Generalabout custom memory managementmemberNing Cao7 Jan '03 - 15:50 
    GeneralMemory leak detectionmemberYud27 Feb '01 - 21:48 
    GeneralI also meet this problem, can anyone give me some idea?memberbenben8 Dec '03 - 15:38 
    QuestionATL support?memberMichael Groeger17 Jan '01 - 22:50 
    AnswerRe: ATL support?memberAudrius Vasiliauskas17 Jan '01 - 23:22 
    GeneralRe: ATL support?memberMichael Groeger6 Feb '01 - 2:42 
    GeneralRe: ATL support?sussStuart Tett18 May '05 - 7:19 
    GeneralRe: ATL support?memberYud27 Feb '01 - 21:43 
    GeneralDoesn't work all the timesussDaníel B. Sigurgeirsson10 Oct '00 - 6:49 
    GeneralPRB: _CRTDBG_MAP_ALLOC Does Not Work as Documented sussMartin Ziacek15 Sep '00 - 8:30 
    GeneralRe: PRB: _CRTDBG_MAP_ALLOC Does Not Work as DocumentedsussAnonymous2 Nov '00 - 22:05 
    GeneralRe: PRB: _CRTDBG_MAP_ALLOC Does Not Work as DocumentedsussAnonymous2 Nov '00 - 22:05 
    GeneralRe: PRB: _CRTDBG_MAP_ALLOC Does Not Work as DocumentedsussAnonymous2 Nov '00 - 22:06 
    QuestionHow about DEBUG_NEWsussJRB12 Sep '00 - 5:11 
    AnswerRe: How about DEBUG_NEWsussAudrius Vasiliauskas13 Sep '00 - 22:34 
    QuestionHow about DEBUG_NEWsussJRB12 Sep '00 - 5:10 

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

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