Discussion:
_CRTDBG_MAP_ALLOC has no effect?
(too old to reply)
Al
2003-10-17 10:01:44 UTC
Permalink
I'm having problems tracking down memory leaks in my MFC
application. I'm following the instructions in MSDN
article "Detecting and Isolating Memory Leaks Using
Microsoft Visual C++" but I only ever get the basic
information when memory leaks are detected. It's as
if "#define _CRTDBG_MAP_ALLOC" is being ignored so I
don't get file and line number information.

I produced a noddy MFC SDI application to test this:

In LeakTest.h, I have
#define _CRTDBG_MAP_ALLOC
#include <stdlib.h>
#include <crtdbg.h>

In CLeakTestApp::InitInstance(), I have
_CrtSetDbgFlag( _CRTDBG_ALLOC_MEM_DF |
_CRTDBG_LEAK_CHECK_DF);

I also have a Leak menu item which allocates 5555 bytes.

So given that I have defined _CRTDBG_MAP_ALLOC, why does
the leak report say
Detected memory leaks!
Dumping objects ->
{88} normal block at 0x003250A0, 5555 bytes long.
Data: < > CD CD CD CD CD CD CD CD CD CD
CD CD CD CD CD CD
Object dump complete.

instead of something like
Detected memory leaks!
Dumping objects ->
U:\MyProjects\leaktest\mainfrm.cpp(108) : {88} normal
block at 0x003250A0, 5555 bytes long.
Data: < > CD CD CD CD CD CD CD CD CD CD
CD CD CD CD CD CD
Object dump complete.

--- Al.
Al
2003-10-17 11:54:17 UTC
Permalink
As an update, in my noddy test program, I now allocate
some more memory using _malloc_dbg() explicitely, passing
__FILE__ and __LINE__ in.

So the code
lpszRude = (TCHAR*)malloc ( sizeof(TCHAR)*5555 ) ;
lpszRude = (TCHAR*)_malloc_dbg ( sizeof(TCHAR)*5555,
_NORMAL_BLOCK, __FILE__, __LINE__ ) ;

results in the output
Dumping objects ->
U:\LeakTest\MainFrm.cpp(111) : {92} normal block at
0x00326778, 5555 bytes long.
Data: < > CD CD CD CD CD CD CD CD CD CD
CD CD CD CD CD CD
{90} normal block at 0x003250F8, 5555 bytes long.
Data: < > CD CD CD CD CD CD CD CD CD CD
CD CD CD CD CD CD
Object dump complete.

So I can solve the problem by defining my own debug
allocation functions to call _malloc_dbg() etc
explicitely, but why does it not work as stated in MSDN?

--- Al.
Doug Harrison [MVP]
2003-10-17 16:37:08 UTC
Permalink
Post by Al
So I can solve the problem by defining my own debug
allocation functions to call _malloc_dbg() etc
explicitely, but why does it not work as stated in MSDN?
I answered your question yesterday. In case you missed it, here's a link to
it on Google:

http://groups.google.com/groups?selm=p6mtovs2310ih9ak2kag4a3denaevin0c1%404ax.com
--
Doug Harrison
Microsoft MVP - Visual C++
Al.
2003-10-20 09:00:13 UTC
Permalink
-----Original Message-----
Post by Al
So I can solve the problem by defining my own debug
allocation functions to call _malloc_dbg() etc
explicitely, but why does it not work as stated in MSDN?
I answered your question yesterday. In case you missed
it, here's a link to
http://groups.google.com/groups?
selm=p6mtovs2310ih9ak2kag4a3denaevin0c1%404ax.com
--
Doug Harrison
Microsoft MVP - Visual C++
.
Many thanks, I have put #define _CRTDBG_MAP_ALLOC at the
top of stdafx.h and it now appears to behave as expected.
I suppose the fact that the MSDN article said the
declarations must be in the right order should have
prompted me to look through the other includes.

--- Al.

Loading...