说明
使用 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. 有一处内存泄漏时的输出报告(int 型)
写一个有一处内存泄漏的程序,如下:
#include <QCoreApplication>
#include "vld.h"
void testFun()
{
int *ptr = new int(0x55345678);
printf("ptr = %08x, *ptr = %08x", ptr, *ptr);
}
int main(int argc, char *argv[])
{
QCoreApplication a(argc, argv);
testFun();
return a.exec();
}
程序运行时,在标准输出窗会输出以下结果:
ptr = 0127b7a0, *ptr = 55345678
程序运行结束后,检测到了内存泄漏,VLD 会输出以下报告(本例中出现一处内存泄漏),第 1~3 行显示 VLD 运行状态,第 4~21 行显示泄漏内存的详细信息,第 22~24 行总结此次泄漏情况,第 25 行显示 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 1 at 0x0127B7A0: 4 bytes ----------
Leak Hash: 0xEB4D3A14, Count: 1, Total 4 bytes
Call Stack (TID 22408):
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!testFun() + 0x7 bytes
e:\cworkspace\qt 5.9\qtdemo\testvld\main.cpp (16): 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:
78 56 34 55 xV4U.... ........
Visual Leak Detector detected 1 memory leak (40 bytes).
Largest number used: 40 bytes.
Total allocations: 40 bytes.
Visual Leak Detector is now exiting.
第 1 行表示 VLD 读取的配置文件路径,可以根据路径找到该文件,然后更改里面的相关配置,获得想要的效果。
第 2 行表示 VLD 2.5.1 在程序中初始化成功。
第 3 行表示本次运行检测到了内存泄漏。
第 4 行中,Block 1
表示本块内存是在堆上分配的第 1 个内存块,0x0127B7A0
表示该内存块的首地址,与标准输出窗输出的 ptr = 0127b7a0
一致,4 bytes
表示该内存块的大小,这一行输出了泄漏内存块的地址信息和大小信息。
第 5 行中,Leak Hash: 0xEB4D3A14
是由泄漏块大小及调用堆栈信息计算出的唯一标识符,如果在报告中看到相同的 Leak Hash
,这表示这些泄漏块具有相同大小和相同的调用堆栈。Count: 1
是发生泄漏的计数,使用默认配置时全部等于 1,可以将配置文件中的参数 AggregateDuplicates
设置为 yes
来合并显示具有相同 Leak Hash
值的的泄漏块信息。Total 4 bytes
是此内存块的泄漏大小,与第 4 行一致。这一行输出了泄漏内存块的唯一标识符、泄漏频次、大小信息。
第 6 行中,Call Stack
表示接下来的几行是产生泄漏的调用堆栈,(TID 22408)
表示产生此内存泄漏块函数所在线程的 TID
为 22408
,据此来指示发生内存泄漏的线程。在调试多线程程序时,TID
信息很有帮助,它能帮助确定泄漏所在线程。
第 7 行中,ucrtbased.dll
是一个系统库,提供了各种标准 C 和 C++ 函数的实现,包括 malloc()
,这个函数用于在运行时动态分配内存。它处于调用栈顶,表示此内存块是使用 ucrtbased.dll
库中的 malloc()
分配的,然后传递给第 8 行 testVLD.exe
程序中的 operator new()
。
第 8 行中,f:\dd\vctools\crt\vcstartup\src\heap\new_scalar.cpp
是从 MSVC
中获取的调试信息,这个路径是内置在 Visual C++ Runtime Library
中的,并不代表 new_scalar.cpp
的真实路径,它的真实路径一般在 Visual Studio
的安装目录下。在我电脑上:
Visual Studio 2015
使用的new_scalar.cpp
文件真实路径为C:\Program F