Click here to Skip to main content
15,867,704 members
Articles / Desktop Programming / MFC

Catching Memory Leaks

Rate me:
Please Sign up or sign in to vote.
3.08/5 (11 votes)
4 Oct 2000 220.6K   1.5K   48   39
How catch memory leaks with very little effort

Introduction

I would like share with you some experience in catching memory leaks. In most cases, it is a long process 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':

C++
#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 'Output' window, 'Debug' tab the following message.

C++
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 its 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.


Written By
Technical Lead Doclogix
Lithuania Lithuania
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
GeneralUnhandled exception in ntdll Pin
ed welch16-Apr-09 12:59
ed welch16-Apr-09 12:59 
GeneralRe: Unhandled exception in ntdll Pin
Audrius Vasiliauskas18-Apr-09 6:53
Audrius Vasiliauskas18-Apr-09 6:53 
Generalerror C2065: 'alloca' : undeclared identifier Pin
safety_ruk25-May-06 22:13
safety_ruk25-May-06 22:13 
GeneralRe: error C2065: 'alloca' : undeclared identifier Pin
akulkarni19-Jul-06 11:42
akulkarni19-Jul-06 11:42 
GeneralRe: error C2065: 'alloca' : undeclared identifier Pin
akulkarni19-Jul-06 11:42
akulkarni19-Jul-06 11:42 
Questionafxpriv.h Pin
ehh8-Sep-05 8:49
ehh8-Sep-05 8:49 
QuestionHow to find real leaked point Pin
murakami15-Aug-05 22:44
murakami15-Aug-05 22:44 
AnswerRe: How to find real leaked point Pin
Tim Read29-May-06 6:23
Tim Read29-May-06 6:23 
AnswerRe: How to find real leaked point Pin
murakami29-May-06 17:01
murakami29-May-06 17:01 
GeneralRe: How to find real leaked point Pin
Tim Read30-May-06 23:20
Tim Read30-May-06 23:20 
GeneralDebugging a DLL - no StdAfx.h available Pin
bschorre30-Mar-05 2:00
bschorre30-Mar-05 2:00 
Hi,

I'm writing a DLL and the code looks cool.

So I tried to use the code but there is a problem I don't know to solve. I'm not using a file named stdfax.h. How do I have to implement tho code to use it.
I tried it like this.
<br />
//#include "stdafx.h"<br />
//#include "CrtDbg.h"<br />
#include "MemDiff.h"<br />

But this isn't working.


Can anyone help me?


THX
Bjoern
GeneralAn extremely useful tip Pin
Anonymous11-Apr-04 19:52
Anonymous11-Apr-04 19:52 
GeneralRe: An extremely useful tip Pin
worksopbenny7-Jun-04 22:54
worksopbenny7-Jun-04 22:54 
GeneralSoooooooo Coooooool Pin
internal10-Jun-04 16:28
internal10-Jun-04 16:28 
GeneralRe: An extremely useful tip Pin
peterboulton9-Dec-04 1:24
professionalpeterboulton9-Dec-04 1:24 
GeneralRe: An extremely useful tip Pin
RainKingFool4-Oct-05 15:43
RainKingFool4-Oct-05 15:43 
Generalabout VC7 Pin
Muhammad Ahmed5-Feb-04 0:14
Muhammad Ahmed5-Feb-04 0:14 
GeneralNot Getting the path where actully memory leak occuring Pin
Jaymin5-Aug-03 2:26
Jaymin5-Aug-03 2:26 
GeneralMemory Leak Pin
Sivakumar R27-Mar-03 23:59
Sivakumar R27-Mar-03 23:59 
Generalabout call stack Pin
Ning Cao7-Jan-03 15:55
Ning Cao7-Jan-03 15:55 
GeneralRe: about call stack Pin
Mike Nordell7-Jan-03 16:25
Mike Nordell7-Jan-03 16:25 
GeneralRe: about call stack Pin
Ning Cao7-Jan-03 16:35
Ning Cao7-Jan-03 16:35 
GeneralRe: about call stack Pin
Kita24-Jun-03 21:33
Kita24-Jun-03 21:33 
Generalabout custom memory management Pin
Ning Cao7-Jan-03 15:50
Ning Cao7-Jan-03 15:50 
GeneralMemory leak detection Pin
27-Feb-01 21:48
suss27-Feb-01 21:48 

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.