views:

384

answers:

2

I found a very interesting memory leak detector by using Visual C++.

http://www.codeproject.com/KB/applications/visualleakdetector.aspx

I try it out, but cannot make it works to detect a memory leak code.

I am using MS Visual Studio 2008. Any step I had missed out?

#include "stdafx.h"
#include "vld.h"
#include <iostream>

void fun() {
    new int[1000];
}

int _tmain(int argc, _TCHAR* argv[])
{
    fun();
    std::cout << "lead?" << std::endl;
    getchar();
    return 0;
}

The output when I run in debug mode is :

...
...
'Test.exe': Loaded 'C:\WINDOWS\WinSxS\x86_Microsoft.VC80.CRT_1fc8b3b9a1e18e3b_8.0.50727.4053_x-ww_e6967989\msvcr80.dll', Symbols loaded.
'Test.exe': Loaded 'C:\WINDOWS\system32\msvcrt.dll', Symbols loaded (source information stripped).
'Test.exe': Loaded 'C:\WINDOWS\WinSxS\x86_Microsoft.VC90.DebugCRT_1fc8b3b9a1e18e3b_9.0.30729.1_x-ww_f863c71f\msvcp90d.dll', Symbols loaded.
'Test.exe': Loaded 'C:\Program Files\Visual Leak Detector\bin\dbghelp.dll', Symbols loaded (source information stripped).
Visual Leak Detector Version 1.9d installed.
No memory leaks detected.
Visual Leak Detector is now exiting.
The program '[5468] Test.exe: Native' has exited with code 0 (0x0).
A: 

My guess is that since new int[1000] is not assigned to anything, the compiler optimized the code and removed the memory allocation part. ( my guess !)

VC6 clearly shows it as memory leak.

Dumping objects ->
{69} normal block at 0x00345028, 4000 bytes long.
 Data: <                > CD CD CD CD CD CD CD CD CD CD CD CD CD CD CD CD 
Object dump complete.
aJ
A: 

If I remember correctly, you still get the standard Visual Studio memory leak report when you use VLD, and you didn't get it in this case, so I agree with aj, the allocation probably isn't happening. Perhaps try to assign the array to something?

You shouldn't need to do anything other than setting up the paths and including vld.h in your project to get VLD to work.

Alex - Aotea Studios