Click here to Skip to main content
13,145,603 members (46,646 online)
Click here to Skip to main content
Add your own
alternative version


55 bookmarked
Posted 21 May 2002

Detailed memory leak dumps from a console app

, 21 May 2002
Rate this:
Please Sign up or sign in to vote.
How to get path and line number info for memory leaks, in a console app


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


#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__;

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.


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.


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

Chris Losinger
Software Developer
United States United States
Chris Losinger is the president of Smaller Animals Software, Inc..

You may also be interested in...


Comments and Discussions

GeneralTHIS_FILE redefinition Problems Pin
Anonymous5-Jan-04 16:37
sussAnonymous5-Jan-04 16:37 
GeneralRe: THIS_FILE redefinition Problems Pin
Anonymous5-Jan-04 17:23
sussAnonymous5-Jan-04 17:23 
GeneralRe: THIS_FILE redefinition Problems Pin
Chris Losinger6-Jan-04 1:32
memberChris Losinger6-Jan-04 1:32 
GeneralRe: THIS_FILE redefinition Problems Pin
Anonymous6-Jan-04 14:14
sussAnonymous6-Jan-04 14:14 
GeneralRe: THIS_FILE redefinition Problems Pin
Anonymous6-Jan-04 14:40
sussAnonymous6-Jan-04 14:40 
GeneralRe: THIS_FILE redefinition Problems Pin
Chris Losinger6-Jan-04 14:53
memberChris Losinger6-Jan-04 14:53 
GeneralRe: THIS_FILE redefinition Problems Pin
Anonymous7-Jan-04 3:11
sussAnonymous7-Jan-04 3:11 
GeneralRe: THIS_FILE redefinition Problems Pin
Chris Losinger7-Jan-04 3:39
memberChris Losinger7-Jan-04 3:39 
GeneralRe: THIS_FILE redefinition Problems Pin
Anonymous7-Jan-04 4:34
sussAnonymous7-Jan-04 4:34 
Aha! All problems solved!

At first I wasn't using an ultra simple project to test it on, merly a simple one. Note in my first post I said:
"...I tried substituting #define THIS_FILE __FILE__. ..."

In the post immediatly after that, dated 23:23 5 Jan '04 as far as I can see, I list a whole host of errors that I got when I tried it. They didn't make a whole lot of sense to me (and I've using VC++ for a long while now), so I assumed it was some macro complication.

I've found out now that they cropped up not because they were caused by the #define THIS_FILE __FILE__, but rather they were just 'revealed' because the #define fix succeeded, and allowed compilation to get to them...

I've been tinkering with an ultra simple project since, and because your suggested use of the #define fix worked in the ultra simple project and not in the simple project, I wanted to know why...

Long storey slightly shorter, those errors in the 'new' file cropped up because the order of the includes in main.cpp was:

#include "header1.h"<br />
#include <iostream><br />
#include <debug_crt.h>

I remember you advising in the article to place the debug_crt.h (in my case) after all previous #includes. Well that #include < iostream > (which wasn't in the ultra simple project) effectively isn't before it, because header1.h contains debug_crt.h and of course it is before debug_crt.h.

I'm not quite sure of the whys, or the exact causes for the errors, all I know is:

// All normal headers first<br />
#include < iostream ><br />
// All headers using debug_crt.h next<br />
#include "header1.h"<br />
// And finally debug_crt.h last<br />
#include < debug_crt.h >

Fixes 'em!
P.S. sometimes one needs to do a 'rebuild all', VC++ sometimes doesn't do a build normally. I'm getting 'operator DEBUG_NEW not recognized' errors, for example. I'm not sure if it's the .h files with declarations and definitions, or not...

Many thanks for the assist!
GeneralRe: THIS_FILE redefinition Problems Pin
Anonymous7-Jan-04 4:39
sussAnonymous7-Jan-04 4:39 

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.

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web01 | 2.8.170915.1 | Last Updated 22 May 2002
Article Copyright 2002 by Chris Losinger
Everything else Copyright © CodeProject, 1999-2017
Layout: fixed | fluid