-
crtdbg.h and mem leaks
trying to use crtdbg.h to remove memory leaks from my program, I've followed instructions in MSDN here:
http://216.239.53.100/search?q=cache...hl=en&ie=UTF-8
It works, but for some reason, instead of getting the filename and the line number where memory allocation happens, it always points to crtdbg.h, so I can get any useful info out of it.
Code:
d:\microsoft visual studio .net\vc7\include\crtdbg.h(689) :
{198} normal block at 0x1190D4D0, 28 bytes long.
Am I missing something?
:confused:
-
Just a guess, but is this at the top of your cpp file (after the includes)?
Code:
#ifdef _DEBUG
#undef THIS_FILE
static char THIS_FILE[]=__FILE__;
#define new DEBUG_NEW
#endif
-
I tried to add those lines, but I got a compiler error, DEBUG_NEW undeclared identifier.
Where is DEBUG_NEW supposed to be defined? It's not in crtdbg.h
-
I'm not sure, it just works for me. What if you just remove the line about DEBUG_NEW?
I know that in our setup, when the file and line number are not displayed it is almost always because someone forgot to put those lines at the top of the cpp file.
-
Thanks for the response :)
while going through ctrdbg.h I found:
Code:
/* Reports with file/line info */
#define _RPTF0(rptno, msg) \
_RPT_BASE((rptno, __FILE__, __LINE__, NULL, "%s", msg))
So I guess it uses __FILE__ ...
I also searched MSDN for DEBUG_NEW and it turned out to be an MFC thing, so it's not defined because I use Win32 API.
I tried to add:
Code:
#ifdef _DEBUG
#undef THIS_FILE
static char THIS_FILE[]=__FILE__;
#endif
but I didn't get actual filenames or line numbers :(
Anyway, Should I add all these stuff (crtdbg.h defines / your defines) in every .cpp file I have, or it's enough to add it to main.cpp (where WinMain is)?
I tried to add them to the file where I suspect leaks but I got the same result.
Thanks in advance.
-
Problem solved :) I found the solution on page 4 after trying every possible keyword combo on Google.
I needed to add:
Code:
#define CRTDBG_MAP_ALLOC
#include <stdlib.h>
#include <crtdbg.h>
// The most important line
#define new new(_NORMAL_BLOCK, __FILE__, __LINE__)
to every .cpp file I had.
Thanks for your efforts :)