Click here to Skip to main content
15,884,353 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 221K   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

 
GeneralI also meet this problem, can anyone give me some idea? Pin
benben8-Dec-03 15:38
benben8-Dec-03 15:38 
QuestionATL support? Pin
Michael Groeger17-Jan-01 22:50
Michael Groeger17-Jan-01 22:50 
AnswerRe: ATL support? Pin
Audrius Vasiliauskas17-Jan-01 23:22
Audrius Vasiliauskas17-Jan-01 23:22 
GeneralRe: ATL support? Pin
Michael Groeger6-Feb-01 2:42
Michael Groeger6-Feb-01 2:42 
GeneralRe: ATL support? Pin
Stuart Tett18-May-05 7:19
sussStuart Tett18-May-05 7:19 
GeneralRe: ATL support? Pin
27-Feb-01 21:43
suss27-Feb-01 21:43 
GeneralDoesn't work all the time Pin
Daníel B. Sigurgeirsson10-Oct-00 6:49
Daníel B. Sigurgeirsson10-Oct-00 6:49 
GeneralPRB: _CRTDBG_MAP_ALLOC Does Not Work as Documented Pin
Martin Ziacek15-Sep-00 8:30
Martin Ziacek15-Sep-00 8:30 
GeneralRe: PRB: _CRTDBG_MAP_ALLOC Does Not Work as Documented Pin
Anonymous2-Nov-00 22:05
Anonymous2-Nov-00 22:05 
GeneralRe: PRB: _CRTDBG_MAP_ALLOC Does Not Work as Documented Pin
Anonymous2-Nov-00 22:05
Anonymous2-Nov-00 22:05 
GeneralRe: PRB: _CRTDBG_MAP_ALLOC Does Not Work as Documented Pin
Anonymous2-Nov-00 22:06
Anonymous2-Nov-00 22:06 
QuestionHow about DEBUG_NEW Pin
JRB12-Sep-00 5:11
JRB12-Sep-00 5:11 
AnswerRe: How about DEBUG_NEW Pin
Audrius Vasiliauskas13-Sep-00 22:34
Audrius Vasiliauskas13-Sep-00 22:34 
QuestionHow about DEBUG_NEW Pin
JRB12-Sep-00 5:10
JRB12-Sep-00 5:10 

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.