Unable to understand the memory leak?

I'm declaring a simple object of type StackElement. But, when I use the memory leak detection provided with Visual Studio 2008, it detects memory leaks. Can anyone please guide why this is happening ?


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
31
32
33
34
35
36
37
38
39
#include "stdafx.h"
#define _CRTDBG_MAP_ALLOC
#include <stdlib.h>
#include <crtdbg.h>
struct StackElement
{
	int*		BitMap;
	double*		LLRMap;
	int			phi;
	int			capn;
	int			smalln;
	StackElement()
	{
		capn = 2;
		smalln = 1;
		phi = 0;
		BitMap = new int[4];
		LLRMap = new double[3];
	}
	StackElement(int icapn, int ismalln, int iphi)
	{
		phi = iphi;
		capn = icapn;
		smalln = ismalln;
		BitMap = new int[capn*(smalln+1)];
		LLRMap = new double[2*capn-1];
	}
	
	~StackElement()
	{
		delete [] BitMap;
		delete [] LLRMap;
	}
};
int _tmain(int argc, _TCHAR* argv[])
{
	StackElement vStackElement(16, 4, 0);
	_CrtDumpMemoryLeaks();
}

Output of the memory leak detector:

Detected memory leaks!
Dumping objects ->
{121} normal block at 0x0093AA98, 248 bytes long.
Data: < > CD CD CD CD CD CD CD CD CD CD CD CD CD CD CD CD
{120} normal block at 0x0093A918, 320 bytes long.
Data: < > CD CD CD CD CD CD CD CD CD CD CD CD CD CD CD CD
Object dump complete.
closed account (Dy7SLyTq)
is _CrtDumpMemoryLeaks() the software you use to detect memory leaks (i dont use vs so i wouldnt know)? if it is, could it be because it is called before vStackElement goes out of scope, where the memory is freed?
Yes DTSCode. VS already has this library. I am not sure if that would be the case. Because, they asked us in their documentation to use _CrtDumpMemoryLeaks() at the exit point of the application. If my definition of "exit point of the application" is correct, this is what they want us to do.
Following is the documentation that I used for the VS Memory Leak detector. It says

After you have enabled the debug heap functions by using these statements, you can place a call to _CrtDumpMemoryLeaks before an application exit point to display a memory-leak report when your application exits:


http://msdn.microsoft.com/en-us/library/vstudio/x98tx3cf.aspx
closed account (Dy7SLyTq)
try this and let me know what happens:
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
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
#include "stdafx.h"
#define _CRTDBG_MAP_ALLOC
#include <stdlib.h>
#include <crtdbg.h>
struct StackElement
{
	int*		BitMap;
	double*		LLRMap;
	int			phi;
	int			capn;
	int			smalln;
	StackElement()
	{
		capn = 2;
		smalln = 1;
		phi = 0;
		BitMap = new int[4];
		LLRMap = new double[3];
	}
	StackElement(int icapn, int ismalln, int iphi)
	{
		phi = iphi;
		capn = icapn;
		smalln = ismalln;
		BitMap = new int[capn*(smalln+1)];
		LLRMap = new double[2*capn-1];
	}
	
	~StackElement()
	{
		delete [] BitMap;
		delete [] LLRMap;
	}
};

void test()
{
	StackElement vStackElement(16, 4, 0);
}

int _tmain(int argc, _TCHAR* argv[])
{
     test();
	_CrtDumpMemoryLeaks();
}
You are right ON :) No memory leaks.. when test() finished, I guess it cleaned up before the memory detector function was called. Thanks :)
closed account (Dy7SLyTq)
no problem. however, so you can call stuff in _tmain, i would try atexit(_CrtDumpMemoryLeaks); in <cstdlib> and that might work
Topic archived. No new replies allowed.