memory leak detect using _CrtDumpMemoryLeaks

Aug 4, 2010 at 9:40pm
I am trying to detect memory leak using _CrtDumpMemoryLeaks in Visual Studio 2008. The problem is that it reports the position (file and line number) of some memory leaks, but not all of them.

I already include the following code in all the files in the project, so it seems very weird, since it is working "partially".

1
2
3
4
5
6
7
#define _CRTDBG_MAP_ALLOC
#include <iostream>
#include <crtdbg.h>
#ifdef _DEBUG
#define  DEBUG_NEW new(_NORMAL_BLOCK, __FILE__, __LINE__)
#define new DEBUG_NEW
#endif 


Has anyone seen this kind of problem before? Any help will be appreciated.
Last edited on Aug 4, 2010 at 9:41pm
Aug 5, 2010 at 1:36am
If memory is allocated in code without debug information, such as code within a library, there can be no file and line to report, because that information doesn't exist.
Aug 5, 2010 at 1:47am
The leak detection at the end of the program shows the allocation number. If your leak is at the same allocation for each run, you can break into the program when the allocation is being made.
Aug 5, 2010 at 3:58am
Thanks guys, it helps me to understand the problem better.
kbw: Do you mean to set _crtBreakAlloc to be the allocation number I want to identify? It indeed breaks when the allocation number is reached, but still does not provide any location information.
Aug 5, 2010 at 6:04pm
If it breaks in the debugger, you'll have a call stack, and you'll see the allocation being made that isn't being freed. In short, you have everything.
Aug 8, 2010 at 4:03am
thanks guys.

maybe it helps if I have a very concrete example.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
#include <iostream>
#include <vector>

#define _CRTDBG_MAP_ALLOC
#include <crtdbg.h>
#ifdef _DEBUG
#define DEBUG_NEW new(_NORMAL_BLOCK, __FILE__, __LINE__)
#define new DEBUG_NEW
#endif

using namespace std;

int main( )   
{
_CrtSetDbgFlag ( _CRTDBG_ALLOC_MEM_DF | _CRTDBG_LEAK_CHECK_DF );
int Y = 1;
int X = 2;
int** superevil = new int*[Y];
for(int i = 0; i < Y; ++i)
	superevil[i] = new int[X];

superevil[0][2] = 1;

/*for(int i = 0; i < Y; ++i)
  delete[] superevil[i];
delete[] superevil;*/

 _CrtDumpMemoryLeaks();
return 0;
}


as you can see, I comment out the delete command and the memory allocated by new is not freed, so memory leak is detected with the a nice report with location information:
Detected memory leaks!
Dumping objects ->
c:\...\visual studio 2008\projects\memorytest\memorytest\memorytest.cpp(41) : {122} normal block at 0x00336658, 12 bytes long.
 Data: <            > CD CD CD CD CD CD CD CD 01 00 00 00 
c:\...\visual studio 2008\projects\memorytest\memorytest\memorytest.cpp(39) : {121} normal block at 0x00336618, 4 bytes long.
 Data: <Xf3 > 58 66 33 00 
Object dump complete.


Then I uncomment the delete statements, while I expect no memory leak, I get the report without location information:
Detected memory leaks!
Dumping objects ->
{120} normal block at 0x003365D8, 4 bytes long.
 Data: <    > 00 00 00 00 
Object dump complete.


Can anyone tell me is there indeed still a memory leak? if so, where is it? why no location information is reported?

Thanks.
Last edited on Aug 8, 2010 at 4:03am
Aug 8, 2010 at 8:11am
How about if you change this line:
1
2
superevil[0][2] = 1; //error out of bounds array access???
Aug 8, 2010 at 10:09am
You need to call _CrtSetBreakAlloc(nnn);
nnn is the allocation number that you want the app to stop at. You get this from the list of unfreed blocks at the end of the program, 120 in the example above.
Aug 10, 2010 at 3:32am
ops, sorry, I made a stupid mistake... Thanks guys!
Topic archived. No new replies allowed.