欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

内存泄漏调试(2)——VLD

程序员文章站 2022-07-14 18:38:31
...

原创文章,转载请注明出处


目录


Visual Leak Detector(VLD)

Visual Leak Detector(VLD)是一款用于VC的免费开源的内存检测工具,通过内存检测生成相应的报告。
报告内容包括:发生内存泄漏的文件和行号;内存泄漏点的调用堆栈;内存泄漏的完整数据等。

调试过程

环境:VS2015 , vld2.3
vld下载地址:http://vld.codeplex.com/

编译版本安装步骤如下:

  1. 将vld安装的bin目录下的dbghelp.dll,Microsoft.DTfW.DHL.manifest,vld_x86.dll拷贝到运行的工程目录下(或是添加到环境变量),否则会出现“计算机丢失dbghelp.dll ”和“应用程序无法正常启动(0xc0150002)”的问题;
  2. 在需要检测内存泄漏的代码文件里#include “vld.h”即可。

源文件安装:

直接下载vld的源程序,将对应的.h文件拷贝到VS的include目录下;将.lid文件拷贝到VS的lib目录下。

可以在安装好的vld目录下编辑vld.ini文件,修改ReportTo = both可以将内存泄漏的报告打印在文件中。

测试用例:

#include "vld.h"
int main() {
       int* p_int = new int(1234);
       char* p_char = new char('a');
       return 1;
}

在Debug模式下运行,内存泄漏报告

Visual Leak Detector Version 2.3 installed.
Outputting the report to the debugger and to E:\wzcGit\testleak\testleak\memory_leak_report.txt
WARNING: Visual Leak Detector detected memory leaks!
———- Block 59 at 0x003754D0: 40 bytes ———-
Call Stack:
d:\th\minkernel\crts\ucrt\src\appcrt\heap\debug_heap.cpp (359): testleak.exe!heap_alloc_dbg_internal + 0x12 bytes
d:\th\minkernel\crts\ucrt\src\appcrt\heap\debug_heap.cpp (450): testleak.exe!heap_alloc_dbg + 0x15 bytes
d:\th\minkernel\crts\ucrt\src\appcrt\heap\debug_heap.cpp (491): testleak.exe!_malloc_dbg + 0x15 bytes
d:\th\minkernel\crts\ucrt\src\appcrt\heap\malloc.cpp (18): testleak.exe!malloc + 0xF bytes
f:\dd\vctools\crt\vcstartup\src\heap\new_scalar.cpp (19): testleak.exe!operator new + 0x9 bytes
e:\wzcgit\testleak\testleak\test.cpp (15): testleak.exe!main + 0x7 bytes
f:\dd\vctools\crt\vcstartup\src\startup\exe_common.inl (74): testleak.exe!invoke_main + 0x1B bytes
f:\dd\vctools\crt\vcstartup\src\startup\exe_common.inl (264): testleak.exe!__scrt_common_main_seh + 0x5 bytes
f:\dd\vctools\crt\vcstartup\src\startup\exe_common.inl (309): testleak.exe!__scrt_common_main
f:\dd\vctools\crt\vcstartup\src\startup\exe_main.cpp (17): testleak.exe!mainCRTStartup
0x77F1EE6C (File and line number not available): kernel32.dll!BaseThreadInitThunk + 0x12 bytes
0x77DC399B (File and line number not available): ntdll.dll!RtlInitializeExceptionChain + 0xEF bytes
0x77DC396E (File and line number not available): ntdll.dll!RtlInitializeExceptionChain + 0xC2 bytes
Data:
00 00 00 00 00 55 37 00 00 00 00 00 00 00 00 00 …..U7. ……..
01 00 00 00 04 00 00 00 3B 00 00 00 FD FD FD FD …….. ;…….
D2 04 00 00 FD FD FD FD …….. ……..

———- Block 60 at 0x00375500: 37 bytes ———-
Call Stack:
d:\th\minkernel\crts\ucrt\src\appcrt\heap\debug_heap.cpp (359): testleak.exe!heap_alloc_dbg_internal + 0x12 bytes
d:\th\minkernel\crts\ucrt\src\appcrt\heap\debug_heap.cpp (450): testleak.exe!heap_alloc_dbg + 0x15 bytes
d:\th\minkernel\crts\ucrt\src\appcrt\heap\debug_heap.cpp (491): testleak.exe!_malloc_dbg + 0x15 bytes
d:\th\minkernel\crts\ucrt\src\appcrt\heap\malloc.cpp (18): testleak.exe!malloc + 0xF bytes
f:\dd\vctools\crt\vcstartup\src\heap\new_scalar.cpp (19): testleak.exe!operator new + 0x9 bytes
e:\wzcgit\testleak\testleak\test.cpp (16): testleak.exe!main + 0x7 bytes
f:\dd\vctools\crt\vcstartup\src\startup\exe_common.inl (74): testleak.exe!invoke_main + 0x1B bytes
f:\dd\vctools\crt\vcstartup\src\startup\exe_common.inl (264): testleak.exe!__scrt_common_main_seh + 0x5 bytes
f:\dd\vctools\crt\vcstartup\src\startup\exe_common.inl (309): testleak.exe!__scrt_common_main
f:\dd\vctools\crt\vcstartup\src\startup\exe_main.cpp (17): testleak.exe!mainCRTStartup
0x77F1EE6C (File and line number not available): kernel32.dll!BaseThreadInitThunk + 0x12 bytes
0x77DC399B (File and line number not available): ntdll.dll!RtlInitializeExceptionChain + 0xEF bytes
0x77DC396E (File and line number not available): ntdll.dll!RtlInitializeExceptionChain + 0xC2 bytes
Data:
D0 54 37 00 00 00 00 00 00 00 00 00 00 00 00 00 .T7….. ……..
01 00 00 00 01 00 00 00 3C 00 00 00 FD FD FD FD …….. <…….
61 FD FD FD FD a……. ……..

Visual Leak Detector detected 2 memory leaks (77 bytes).
Largest number used: 11554 bytes.
Total allocations: 16495 bytes.
Visual Leak Detector is now exiting.

相关标签: 内存泄露