Click here to Skip to main content
15,878,945 members
Articles / Desktop Programming / MFC
Article

Detailed memory leak dumps from a console app

Rate me:
Please Sign up or sign in to vote.
4.79/5 (16 votes)
21 May 20021 min read 203.4K   57   45
How to get path and line number info for memory leaks, in a console app

Introduction

Finding memory leaks in a non-MFC app can be difficult because the debugger doesn't display path and line number info in the debug window. But, it's quite easy to enable that info. Here's how:

Step 1

Copy this to a file called LeakWatcher.h

#ifndef IMWATCHINGYOULEAK
#define IMWATCHINGYOULEAK

#include <crtdbg.h>

#ifdef _DEBUG
void* operator new(size_t nSize, const char * lpszFileName, int nLine)
{
    return ::operator new(nSize, 1, lpszFileName, nLine);
}
#define DEBUG_NEW new(THIS_FILE, __LINE__)

#define MALLOC_DBG(x) _malloc_dbg(x, 1, THIS_FILE, __LINE__);
#define malloc(x) MALLOC_DBG(x)

#endif // _DEBUG

#endif // #include guard

Step 2

Call _CrtDumpMemoryLeaks(); at the end of your program (or wherever you want to see outstanding memory allocations).

Steps 3...N

Add this to each of your source files (after your last #include) :

#include "LeakWatcher.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

What does it do?

This does the same thing an MFC app does - it provides the path and line number of each allocation to the C-runtime allocation routines. Those routines store the path and line number for each allocation, and spit them out when you call _CrtDumpMemoryLeaks();. You should recognize that little chunk of code in step 3 from every source file that the AppWizard (and ClassWizard, mostly) has ever created.

Why not just use _CRTDBG_MAP_ALLOC?

#define _CRTDBG_MAP_ALLOC will provide file/line info for leaks caused by malloc, but for leaks with new, it ends up telling you that the leak occurred in crtdbg.h (because that's where MS defined their new) - not really useful.

Credits

This represents about ten minutes of searching through Microsoft's C runtime and MFC source files. So, all of the credit goes to MS. But, all of the blame goes to MS, too, for not making this part of the non-MFC headers. Anyway, have fun and be excellent to each other.

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
Software Developer
United States United States
Chris Losinger was the president of Smaller Animals Software, Inc. (which no longer exists).

Comments and Discussions

 
GeneralRe: Link Errors Pin
Chris Losinger19-Nov-02 6:05
professionalChris Losinger19-Nov-02 6:05 
GeneralRe: Link Errors Pin
Christian Cheney19-Nov-02 6:09
Christian Cheney19-Nov-02 6:09 
GeneralRe: Link Errors Pin
Chris Losinger19-Nov-02 6:19
professionalChris Losinger19-Nov-02 6:19 
GeneralRe: Link Errors Pin
Christian Cheney19-Nov-02 6:22
Christian Cheney19-Nov-02 6:22 
GeneralRe: Link Errors Pin
Christian Cheney19-Nov-02 9:15
Christian Cheney19-Nov-02 9:15 
GeneralRe: Link Errors Pin
DaveDrPop10-Jun-05 3:42
DaveDrPop10-Jun-05 3:42 
GeneralRe: Link Errors Pin
ben staveley-taylor29-Mar-06 0:05
ben staveley-taylor29-Mar-06 0:05 
GeneralRe: Link Errors Pin
Chrabros6-Sep-05 19:53
Chrabros6-Sep-05 19:53 
GeneralYou guys saved my life! Pin
Benjamin Chen25-Oct-02 15:31
Benjamin Chen25-Oct-02 15:31 
GeneralProblem with global variables Pin
Anonymous20-Oct-02 14:01
Anonymous20-Oct-02 14:01 
GeneralRe: Problem with global variables Pin
Christian Cheney19-Nov-02 6:07
Christian Cheney19-Nov-02 6:07 
GeneralRe: Problem with global variables Pin
Christian Cheney19-Nov-02 9:12
Christian Cheney19-Nov-02 9:12 
GeneralMinor Issues Pin
Rick York3-Jun-02 13:06
mveRick York3-Jun-02 13:06 
GeneralExcellent ! Pin
24-May-02 0:27
suss24-May-02 0:27 
GeneralRe: Excellent ! Pin
gunnware21-May-03 3:48
gunnware21-May-03 3:48 
GeneralRe: Excellent ! Pin
Mythago13-Jul-03 4:43
Mythago13-Jul-03 4:43 
GeneralRe: Excellent ! Pin
rbid21-Dec-03 21:16
rbid21-Dec-03 21:16 
General_CRTDBG_MAP_ALLOC Pin
23-May-02 1:10
suss23-May-02 1:10 
GeneralRe: _CRTDBG_MAP_ALLOC Pin
Chris Losinger24-May-02 3:28
professionalChris Losinger24-May-02 3:28 
GeneralWow ! That was quick ! Pin
Joao Vaz22-May-02 7:23
Joao Vaz22-May-02 7:23 

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.