Hi,

I am trying to enable memory leak detection based on the url below.

If I use _CRTDBG_MAP_ALLOC instead of CRTDBG_MAP_ALLOC in a test program, I
can see the offending line numbers and source file get printed out.

However in the program that I am trying to find memory leaks, I get the same
debug output regardless of whether I #define _CRTDBG_MAP_ALLOC or
CRTDBG_MAP_ALLOC. And I only get one line number <dllmodul.cpp(106)> output
instead of every offending (leak causing) line number. Why is that?

// based on

http://msdn.microsoft.com/library/default.asp?url=/library/en-us/vsdebug/html/vxconenablingmemoryleakdetection.asp

#define _CRTDBG_MAP_ALLOC
#include <stdlib.h>
#include <crtdbg.h>
. . .
_CrtDumpMemoryLeaks();

OUTPUT
Detected memory leaks!
Dumping objects ->
{61} normal block at 0x00872680, 7 bytes long.
Data: <MyExename> 43 61 70 47 65 6E 00
{56} normal block at 0x00872748, 33 bytes long.
Data: < C > 00 43 00 CD CD CD CD CD CD CD CD CD CD CD CD CD
{55} normal block at 0x008726E0, 40 bytes long.
Data: < |L > 14 7C 4C 10 16 00 00 00 00 00 00 00 00 00 00 00
dllmodul.cpp(106) : {53} client block at 0x00872590, subtype 0, 64 bytes long.
a CDynLinkLibrary object at $00872590, 64 bytes long
{52} client block at 0x00872508, subtype 0, 64 bytes long.
a CDynLinkLibrary object at $00872508, 64 bytes long
{50} client block at 0x00874F68, subtype 0, 64 bytes long.
a CDynLinkLibrary object at $00874F68, 64 bytes long
Object dump complete.

Thanks!

Re: Enabling Memory Leak Detection by Jochen

Jochen
Fri Jul 22 01:16:06 CDT 2005

Hi malhenry!


> However in the program that I am trying to find memory leaks, I get the same
> debug output regardless of whether I #define _CRTDBG_MAP_ALLOC or
> CRTDBG_MAP_ALLOC. And I only get one line number <dllmodul.cpp(106)> output
> instead of every offending (leak causing) line number. Why is that?

If you use STL or some other 3rd-party lib the build-in leakfinder is
not very usefull...

You can use my leakfinder, which will also display the callstack.
http://blog.kalmbachnet.de/files/LeakFinder-RC2.zip

See also: http://www.codeproject.com/tools/leakfinder.asp

--
Greetings
Jochen

My blog about Win32 and .NET
http://blog.kalmbachnet.de/

Re: Enabling Memory Leak Detection by malhenry

malhenry
Mon Jul 25 10:17:09 CDT 2005

Ok, so I am trying to use your code. I am using VC++ 6.0 and Win XP Pro (32
not 64).
I installed the Platform SDK (to get dbghelp.h), included leakfinder.h,
added leakfinder.cpp and .h to the project and added the following calls in
my main:
InitLeakFinder(OutputVerbose);
DeinitLeakFinder();
Then I had to move the declarations of some of your variables to fix compile
errors.
( HANDLE hT = hThread;
HANDLE hP = hProc;
int frameNum;
int cnt;
)
Now I get: error C2653: 'super' : is not a class or namespace name
Why is the compiler not finding super???

Your code Leakfinder.cpp was too long to post here.

Thanks.

Re: Enabling Memory Leak Detection by Jochen

Jochen
Mon Jul 25 13:02:03 CDT 2005

Hi malhenry!
> Ok, so I am trying to use your code. I am using VC++ 6.0 and Win XP Pro (32
> not 64).

Sorry... I never tested it with VC6... (maybe I sould do it...)


> Now I get: error C2653: 'super' : is not a class or namespace name
> Why is the compiler not finding super???

Just replace "__super" with "CallStackInterface"


--
Greetings
Jochen

My blog about Win32 and .NET
http://blog.kalmbachnet.de/

Re: Enabling Memory Leak Detection by malhenry

malhenry
Mon Jul 25 13:56:01 CDT 2005

Yes it would be helpful to test with VC 6.

Your last reply got me around the compile problems. I ran a trivial test,
but did not get any helpful output. Maybe I am not doing something correctly?

Here is my code:

// TestMemLeak.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"

#include "leakfinder.h"

// enable memory leak detection


#include "TestMemLeak.h"

int main(int argc, char* argv[])
{
double *pDouble = new double( 3.14159 );

InitLeakFinder(OutputVerbose);
// don't execute the next line if you want a leak

// delete pDouble;

TestMem2();
DeinitLeakFinder();

return 0;
}
=====================
// stdafx.cpp : source file that includes just the standard includes
// TestMemLeak.pch will be the pre-compiled header
// stdafx.obj will contain the pre-compiled type information

#include "stdafx.h"

// TODO: reference any additional headers you need in STDAFX.H
// and not in this file
==================

//testmem2.cpp
void TestMem2()
{
int * arrayPtr = new int[10];

return;
}
===========
//testmemleak.h
// prototypes

void TestMem2();
=================

Here is the output provided by your program to the debug window:

Loaded 'ntdll.dll', no matching symbolic information found.
Loaded 'C:\WINDOWS\system32\kernel32.dll', no matching symbolic information
found.
Loaded 'C:\WINDOWS\system32\ole32.dll', no matching symbolic information
found.
Loaded 'C:\WINDOWS\system32\advapi32.dll', no matching symbolic information
found.
Loaded 'C:\WINDOWS\system32\rpcrt4.dll', no matching symbolic information
found.
Loaded 'C:\WINDOWS\system32\gdi32.dll', no matching symbolic information
found.
Loaded 'C:\WINDOWS\system32\user32.dll', no matching symbolic information
found.
Loaded 'C:\WINDOWS\system32\msvcrt.dll', no matching symbolic information
found.
Loaded 'C:\WINDOWS\system32\version.dll', no matching symbolic information
found.
Loaded 'C:\WINDOWS\system32\xpsp2res.dll', no matching symbolic information
found.
Loaded 'C:\WINDOWS\system32\dbghelp.dll', no matching symbolic information
found.
######## CRT-Leaks ########
C:\Projects\TestMemLeak\Debug\TestMemLeak.exe:TestMemLeak.exe
(00400000), size: 233472 (result: 0)
C:\WINDOWS\system32\ntdll.dll:ntdll.dll (7C900000), size: 720896
(result: 0), fileVersion: 5.1.2600.2180
C:\WINDOWS\system32\kernel32.dll:kernel32.dll (7C800000), size: 999424
(result: 0), fileVersion: 5.1.2600.2180
C:\WINDOWS\system32\ole32.dll:ole32.dll (774E0000), size: 1298432
(result: 0), fileVersion: 5.1.2600.2595
C:\WINDOWS\system32\ADVAPI32.dll:ADVAPI32.dll (77DD0000), size: 634880
(result: 0), fileVersion: 5.1.2600.2180
C:\WINDOWS\system32\RPCRT4.dll:RPCRT4.dll (77E70000), size: 593920
(result: 0), fileVersion: 5.1.2600.2180
C:\WINDOWS\system32\GDI32.dll:GDI32.dll (77F10000), size: 286720
(result: 0), fileVersion: 5.1.2600.2180
C:\WINDOWS\system32\USER32.dll:USER32.dll (77D40000), size: 589824
(result: 0), fileVersion: 5.1.2600.2622
C:\WINDOWS\system32\msvcrt.dll:msvcrt.dll (77C10000), size: 360448
(result: 0), fileVersion: 7.0.2600.2180
C:\WINDOWS\system32\VERSION.dll:VERSION.dll (77C00000), size: 32768
(result: 0), fileVersion: 5.1.2600.2180
C:\WINDOWS\system32\xpsp2res.dll:xpsp2res.dll (20000000), size: 2904064
(result: 0), fileVersion: 5.1.2600.2180
C:\WINDOWS\system32\dbghelp.dll:dbghelp.dll (59A60000), size: 659456
(result: 0), fileVersion: 5.1.2600.2180
ERROR: SymGetSymFromAddr, GetLastError: 487 (Address: 0040256F)
ERROR: SymGetLineFromAddr, GetLastError: 487 (Address: 0040256F)
ERROR: SymGetModuleInfo, GetLastError: 87 (Address: 0040256F)
--------------- Key: 52, 40 bytes ---------
Call stack:
0040256F ((module-name not available)): (filename not available):
(function-name not available)
ERROR: SymGetSymFromAddr, GetLastError: 487 (Address: 00406ECF)
ERROR: SymGetLineFromAddr, GetLastError: 487 (Address: 00406ECF)
ERROR: SymGetModuleInfo, GetLastError: 87 (Address: 00406ECF)
00406ECF ((module-name not available)): (filename not available):
(function-name not available)
ERROR: SymGetSymFromAddr, GetLastError: 487 (Address: 00406DF9)
ERROR: SymGetLineFromAddr, GetLastError: 487 (Address: 00406DF9)
ERROR: SymGetModuleInfo, GetLastError: 87 (Address: 00406DF9)
00406DF9 ((module-name not available)): (filename not available):
(function-name not available)
ERROR: SymGetSymFromAddr, GetLastError: 487 (Address: 00406DD6)
ERROR: SymGetLineFromAddr, GetLastError: 487 (Address: 00406DD6)
ERROR: SymGetModuleInfo, GetLastError: 87 (Address: 00406DD6)
00406DD6 ((module-name not available)): (filename not available):
(function-name not available)
ERROR: SymGetSymFromAddr, GetLastError: 487 (Address: 00406D4F)
ERROR: SymGetLineFromAddr, GetLastError: 487 (Address: 00406D4F)
ERROR: SymGetModuleInfo, GetLastError: 87 (Address: 00406D4F)
00406D4F ((module-name not available)): (filename not available):
(function-name not available)
ERROR: SymGetSymFromAddr, GetLastError: 487 (Address: 0040687F)
ERROR: SymGetLineFromAddr, GetLastError: 487 (Address: 0040687F)
ERROR: SymGetModuleInfo, GetLastError: 87 (Address: 0040687F)
0040687F ((module-name not available)): (filename not available):
(function-name not available)
ERROR: SymGetSymFromAddr, GetLastError: 487 (Address: 0040690F)
ERROR: SymGetLineFromAddr, GetLastError: 487 (Address: 0040690F)
ERROR: SymGetModuleInfo, GetLastError: 87 (Address: 0040690F)
0040690F ((module-name not available)): (filename not available):
(function-name not available)
ERROR: SymGetSymFromAddr, GetLastError: 487 (Address: 0040A269)
ERROR: SymGetLineFromAddr, GetLastError: 487 (Address: 0040A269)
ERROR: SymGetModuleInfo, GetLastError: 87 (Address: 0040A269)
0040A269 ((module-name not available)): (filename not available):
(function-name not available)
ERROR: SymGetLineFromAddr, GetLastError: 487 (Address: 7C816D4F)
ERROR: SymGetModuleInfo, GetLastError: 87 (Address: 7C816D4F)
7C816D4F ((module-name not available)): (filename not available):
RegisterWaitForInputIdle
######## COM-Leaks ########
The program 'C:\Projects\TestMemLeak\Debug\TestMemLeak.exe' has exited with
code 0 (0x0).

Thanks.
"Jochen Kalmbach [MVP]" wrote:

> Hi malhenry!
> > Ok, so I am trying to use your code. I am using VC++ 6.0 and Win XP Pro (32
> > not 64).
>
> Sorry... I never tested it with VC6... (maybe I sould do it...)
>
>
> > Now I get: error C2653: 'super' : is not a class or namespace name
> > Why is the compiler not finding super???
>
> Just replace "__super" with "CallStackInterface"
>
>
> --
> Greetings
> Jochen
>
> My blog about Win32 and .NET
> http://blog.kalmbachnet.de/
>

Re: Enabling Memory Leak Detection by Jochen

Jochen
Mon Jul 25 14:23:36 CDT 2005

Hi malhenry!

> C:\Projects\TestMemLeak\Debug\TestMemLeak.exe:TestMemLeak.exe
> (00400000), size: 233472 (result: 0)

It seems that it was not able to read the PDB-file for your EXE...

I will test it with VC5/6 asap...


--
Greetings
Jochen

My blog about Win32 and .NET
http://blog.kalmbachnet.de/