开发者

Usage of VIsual Memory Leak Detector

开发者 https://www.devze.com 2022-12-25 02:41 出处:网络
I found a very interesting memory leak detector by using Visual C++. http://www.codeproject.com/KB/applications/visualleakdetector.aspx

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': Lo开发者_开发知识库aded '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).


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.


I also have undetected "leaks". Here is the full story.

The VS2008 debug heap was reporting lots of leaks on termination that were difficult to track down (as the actual allocations were in MFC code with no stack trace). I tried VLD 2.2 and it worked perfectly to track down 4 leaks (though these were not really a problem being more like memory "splashes", ie allocations of "singleton" objects).

After fixing those VLD now reports "No memory leaks detected" but VS2008 debug heap reports "Detected memory leaks!" then dumps out hundreds of them. Almost all of them occur at vctools\vc7libs\ship\atlmfc\src\mfc\strcore.cpp(141).

It seems VLD does not detect all leaks.

Using: VLD 2.2, VS2008, MFC 9 with Feature Pack installed.

BTW The suggestion above that the allocation in the example is optimized away is wrong as no optimizations are done in debug builds.


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消