说明
使用 VLD 内存泄漏检测工具辅助开发时整理的学习笔记。 同系列文章目录可见 《内存泄漏检测工具》目录
1. 使用方式
在 QT 中使用 VLD 的方法可以查看另外几篇博客:
本次测试使用的环境为:QT 5.9.2,MSVC 2015 32bit,Debug 模式,VLD 版本为 2.5.1,VLD 配置文件不做任何更改使用默认配置,测试工程所在路径为:E:\Cworkspace\Qt 5.9\QtDemo\testVLD
。
2. 有三处内存泄漏时的输出报告
写一个有三处内存泄漏的程序,如下:
#include <QCoreApplication>
#include "vld.h"
void testFun1()
{
int *ptr = new int(0x55345678);
printf("ptr1 = %08x, *ptr1 = %08x.\n", ptr, *ptr);
}
void testFun2()
{
short *ptr = new short(0x4529);
printf("ptr2 = %08x, *ptr2 = %04x.\n", ptr, *ptr);
}
void testFun3()
{
char *ptr = new char[3];
printf("ptr3 = %08x.\n", ptr, *ptr);
}
int main(int argc, char *argv[])
{
QCoreApplication a(argc, argv);
testFun1();
testFun2();
testFun3();
return a.exec();
}
程序运行时,在标准输出窗会输出以下结果:
ptr1 = 00b674b8, *ptr1 = 55345678.
ptr2 = 00b670f8, *ptr2 = 4529.
ptr3 = 00b674e8.
程序运行结束后,检测到了内存泄漏,VLD 会输出以下报告(本例中出现三处内存泄漏),第 1~3 行显示 VLD 运行状态,第 4~19 行显示 testFun2()
函数泄漏内存的详细信息,第 22~37 行显示 testFun1()
函数泄漏内存的详细信息,第 40~55 行显示 testFun3()
函数泄漏内存的详细信息,第 58~60 行总结此次泄漏情况,第 61 行显示 VLD 退出状态。
Visual Leak Detector read settings from: D:\Program Files (x86)\Visual Leak Detector\vld.ini
Visual Leak Detector Version 2.5.1 installed.
WARNING: Visual Leak Detector detected memory leaks!
---------- Block 2 at 0x00B670F8: 2 bytes ----------
Leak Hash: 0xB9FC7D06, Count: 1, Total 2 bytes
Call Stack (TID 14904):
ucrtbased.dll!malloc()
f:\dd\vctools\crt\vcstartup\src\heap\new_scalar.cpp (19): testVLD.exe!operator new() + 0x9 bytes
e:\cworkspace\qt 5.9\qtdemo\testvld\main.cpp (12): testVLD.exe!testFun2() + 0x7 bytes
e:\cworkspace\qt 5.9\qtdemo\testvld\main.cpp (28): testVLD.exe!main()
f:\dd\vctools\crt\vcstartup\src\startup\exe_common.inl (74): testVLD.exe!invoke_main() + 0x1B bytes
f:\dd\vctools\crt\vcstartup\src\startup\exe_common.inl (264): testVLD.exe!__scrt_common_main_seh() + 0x5 bytes
f:\dd\vctools\crt\vcstartup\src\startup\exe_common.inl (309): testVLD.exe!__scrt_common_main()
f:\dd\vctools\crt\vcstartup\src\startup\exe_main.cpp (17): testVLD.exe!mainCRTStartup()
KERNEL32.DLL!BaseThreadInitThunk() + 0x19 bytes
ntdll.dll!RtlGetAppContainerNamedObjectPath() + 0x11E bytes
ntdll.dll!RtlGetAppContainerNamedObjectPath() + 0xEE bytes
Data:
29 45 )E...... ........
---------- Block 1 at 0x00B674B8: 4 bytes ----------
Leak Hash: 0xE622CBED, Count: 1, Total 4 bytes
Call Stack (TID 14904):
ucrtbased.dll!malloc()
f:\dd\vctools\crt\vcstartup\src\heap\new_scalar.cpp (19): testVLD.exe!operator new() + 0x9 bytes
e:\cworkspace\qt 5.9\qtdemo\testvld\main.cpp (6): testVLD.exe!testFun1() + 0x7 bytes
e:\cworkspace\qt 5.9\qtdemo\testvld\main.cpp (27): testVLD.exe!main()
f:\dd\vctools\crt\vcstartup\src\startup\exe_common.inl (74): testVLD.exe!invoke_main() + 0x1B bytes
f:\dd\vctools\crt\vcstartup\src\startup\exe_common.inl (264): testVLD.exe!__scrt_common_main_seh() + 0x5 bytes
f:\dd\vctools\crt\vcstartup\src\startup\exe_common.inl (309): testVLD.exe!__scrt_common_main()
f:\dd\vctools\crt\vcstartup\src\startup\exe_main.cpp (17): testVLD.exe!mainCRTStartup()
KERNEL32.DLL