Visual Leak Detector for Visual C++

手头的项目有历史遗留的内存泄露,每次关闭程序输出窗口就风起云涌层出不穷。

感谢知乎用户程曦铭,终于发现了解决VS下C++内存泄露的好工具,免费&开源。
Visual Leak Detector for Visual C++

直接下载安装,添加include和library目录到VS搜索路径,在任意一个文件里包含vld.h即可。
详情可看官网。

此后如果探查到内存泄露,就会输出完整调用堆栈和数据:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
---------- Block 1199 at 0x04BE1058: 136 bytes ----------
Call Stack:
d:\Foobar\FooLog.cpp (26): FooLog::getInstance
d:\Foobar\FooMain.cpp (75): FooMain::init
f:\dd\vctools\crt_bld\self_x86\crt\src\crtexe.c (578): __tmainCRTStartup
f:\dd\vctools\crt_bld\self_x86\crt\src\crtexe.c (403): WinMainCRTStartup
0x759A3677 (File and line number not available): BaseThreadInitThunk
0x770C9D42 (File and line number not available): RtlInitializeExceptionChain
0x770C9D15 (File and line number not available): RtlInitializeExceptionChain
Data:
9C 33 2D 6B 74 2A 2D 6B C8 11 BE 04 00 00 00 00 .3-kt*-k ........
00 00 00 00 70 14 BB 6C 70 14 BB 6C 00 00 00 00 ....p..l p..l....
00 00 00 00 68 14 BB 6C 68 14 BB 6C 00 00 00 00 ....h..l h..l....
00 00 00 00 6C 14 BB 6C 6C 14 BB 6C 20 12 BE 04 ....l..l l..l....
00 00 00 00 CD 00 CD CD 00 00 00 00 01 CD CD CD ........ ........
68 14 BB 6C 78 33 2D 6B 00 00 00 00 00 00 00 00 h..lx3-k ........
00 00 00 00 01 02 00 00 06 00 00 00 00 00 00 00 ........ ........
00 00 00 00 00 00 00 00 88 11 BE 04 5C 10 BE 04 ........ ....\...
00 00 00 00 20 CD CD CD ........ ........