Click here to Skip to main content
Licence 
First Posted 21 May 2002
Views 147,223
Bookmarked 52 times

Detailed memory leak dumps from a console app

By | 21 May 2002 | Article
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

About the Author

Chris Losinger

Software Developer

United States United States

Member

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

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

 
You must Sign In to use this message board. (secure sign-in)
 
Search this forum  
 FAQ
    Noise  Layout  Per page   
  Refresh
GeneralHit or Miss on the line number reporting... Pinmemberheckknow11:54 24 Dec '06  
GeneralRe: Hit or Miss on the line number reporting... Pinmembersmjones-home10:16 15 Mar '07  
GeneralTwo Problems PinsussAndrew @ Neoteck21:36 4 May '05  
Hi all,
 
I'll preface this by saying I tried out this code on a small application to test it out and it works a treat!
 
However, I've tried using it into a large program written by someone else, who has asked me to maintain the code, and I get a lot of compiler errors and warnings.
 

warning C4291: 'void *__cdecl operator new(unsigned int,const char *,int)' : no matching operator delete found; memory will not be freed if initialization throws an exception

 
Appears for every single time the new operator is called (which is a lot of times!).
 
Secondly, I get errors appearing like this one:
 

c:\program files\microsoft visual studio\vc98\include\fstream(217) : error C2059: syntax error : '&'
c:\program files\microsoft visual studio\vc98\include\fstream(213) : while compiling class-template member function 'void __thiscall std::basic_filebuf >::_Init(struct _iobuf *,enum std::basic_filebuf
 
The line in question in fstream is
new (&_Loc) locale;
which clearly says it must be something wrong with my overloaded new operator.
 
Every CPP file in this program only uses the one header file, although that header file does call several other header files as well. I've included the

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

 
at the very bottom of the main header file.
 
I don't need to be told how bad a practice it is of having ALL the source read out of the same header file. I'm just maintaining this code, I didn't write it.
 
Please help. Finding the source of the memory leaks without this is going to be damn near impossible.
GeneralRe: Two Problems PinsussAndrew @ Neoteck21:37 4 May '05  
GeneralRe: Two Problems PinmemberChris Losinger0:59 5 May '05  
GeneralRe: Two Problems PinsussAndrew @ Neoteck13:34 5 May '05  
GeneralRe: Two Problems PinmemberNosferatus5:53 22 Jul '05  
GeneralRe: Two Problems PinmemberBob Stanneveld21:15 1 Feb '06  
QuestionWhere is memory leak from? Pinmembermorntide16:05 25 Feb '05  
AnswerRe: Where is memory leak from? PinmemberNosferatus5:59 22 Jul '05  
Questionisn't this better? PinsussAnonymous0:17 2 Feb '05  
GeneralWhere to put _CrtDumpMemoryLeaks() for Dialog based applications... Pinsussrbid21:16 21 Dec '04  
GeneralTHIS_FILE redefinition Problems PinsussAnonymous16:37 5 Jan '04  
GeneralRe: THIS_FILE redefinition Problems PinsussAnonymous17:23 5 Jan '04  
GeneralRe: THIS_FILE redefinition Problems PinmemberChris Losinger1:32 6 Jan '04  
GeneralRe: THIS_FILE redefinition Problems PinsussAnonymous14:14 6 Jan '04  
GeneralRe: THIS_FILE redefinition Problems PinsussAnonymous14:40 6 Jan '04  
GeneralRe: THIS_FILE redefinition Problems PinmemberChris Losinger14:53 6 Jan '04  
GeneralRe: THIS_FILE redefinition Problems PinsussAnonymous3:11 7 Jan '04  
GeneralRe: THIS_FILE redefinition Problems PinmemberChris Losinger3:39 7 Jan '04  
GeneralRe: THIS_FILE redefinition Problems PinsussAnonymous4:34 7 Jan '04  
GeneralRe: THIS_FILE redefinition Problems PinsussAnonymous4:39 7 Jan '04  
Generalspecialized "new" is never called Pinmemberblizzymadden12:46 25 Nov '03  
GeneralLink Errors PinsussChristian Cheney5:59 19 Nov '02  
GeneralRe: Link Errors PinmemberChris Losinger6:05 19 Nov '02  

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    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 | Mobile
Web01 | 2.5.120528.1 | Last Updated 22 May 2002
Article Copyright 2002 by Chris Losinger
Everything else Copyright © CodeProject, 1999-2012
Terms of Use
Layout: fixed | fluid