说明
使用 VLD 内存泄漏检测工具辅助开发时整理的学习笔记。本篇介绍 VLD 配置文件中配置项 StartDisabled 的使用方法。同系列文章目录可见 《内存泄漏检测工具》目录
1. 配置文件使用说明
在程序中通过 #include "vld.h"
的方式检测内存泄漏时,VLD 首先会尝试在程序的生成目录下读取 vld.ini
文件,若未读取成功,则会尝试在 VLD 的安装目录下读取 vld.ini
文件,若仍未读取成功,则会使用内置的默认配置,内置的默认配置如果不动源码是无法更改的,因此通过修改相应目录下的 vld.ini
文件来定制 VLD 功能是最好的选择。当配置参数等号右边为空,或者给配置了不合法值时,在使用过程中会被程序重置到默认值。
2. 设置是否禁用自动初始化
参数名:StartDisabled
。
有效赋值:yes
,no
。
默认值:no
。
功能说明:设置是否禁用自动初始化,当 StartDisabled = yes
时,将会禁用 VLD 的自动初始化,此时若想检测程序中的某个函数、某个线程是否存在内存泄漏(即在运行过程中控制 VLD 的开启与关闭),需提前人为地调用 VLDEnable()
函数来进行初始化,当被测试的函数结束后,还可以使用 VLDDisable()
函数来关闭 VLD 内存检测。需注意的是,使用这种人为的控制方法时,检测不到 VLD 关闭期间的内存分配引起的内存泄漏;此外,调用 VLDEnable()
与 VLDDisable()
函数时,仅仅对调用函数的线程有效,对于同一进程的其他的线程无效,除非在每个线程中都进行了调用。详见 Controlling-Leak-Detection-at-Runtime。
2.1 测试代码
#include <QCoreApplication>
#include "vld.h"
void testFun(int i)
{
int *ptr = new int(i);
printf("ptr = %08x, *ptr = %08x.\n", ptr, *ptr);
}
int main(int argc, char *argv[])
{
QCoreApplication a(argc, argv);
testFun(1);
VLDEnable();
testFun(2);
VLDDisable();
testFun(3);
return a.exec();
}
测试环境:QT 5.9.2,MSVC 2015 32bit,Debug 模式,VLD 版本为 2.5.1,VLD 配置文件只对该参数做修改,测试工程所在路径为:E:\Cworkspace\Qt 5.9\QtDemo\testVLD
。
2.2 StartDisabled = no 时的输出
标准输出窗显示:
ptr = 00585f68, *ptr = 00000001.
ptr = 00586088, *ptr = 00000002.
ptr = 005860e8, *ptr = 00000003.
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 0x00585F68: 4 bytes ----------
Leak Hash: 0xA089A5DA, Count: 1, Total 4 bytes
Call Stack (TID 33388):
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 (14): testVLD.exe!main() + 0x7 bytes
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:
01 00 00 00 ........ ........
---------- Block 2 at 0x00586088: 4 bytes ----------
Leak Hash: 0xF090F245, Count: 1, Total 4 bytes
Call Stack (TID 33388):
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 (17): testVLD.exe!main() + 0x7 bytes
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:
02 00 00 00 ........ ........
Visual Leak Detector detected 2 memory leaks (80 bytes).
Largest number used: 80 bytes.
Total allocations: 80 bytes.
Visual Leak Detector is now exiting.
2.3 StartDisabled = yes 时的输出
标准输出窗显示:
ptr = 00797730, *ptr = 00000001.
ptr = 007974f0, *ptr = 00000002.
ptr = 00797550, *ptr = 00000003.
VLD 输出报告:
Visual Leak Detector read settings from: D:\Program Files (x86)\Visual Leak Detector\vld.ini
Visual Leak Detector Version 2.5.1 installed.
Starting with memory leak detection disabled.
WARNING: Visual Leak Detector detected memory leaks!
---------- Block 1 at 0x007974F0: 4 bytes ----------
Leak Hash: 0xF090F245, Count: 1, Total 4 bytes
Call Stack (TID 7920):
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 (17): testVLD.exe!main() + 0x7 bytes
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:
02 00 00 00 ........ ........
Visual Leak Detector detected 1 memory leak (40 bytes).
Largest number used: 40 bytes.
Total allocations: 40 bytes.
Visual Leak Detector is now exiting.
2.4 输出结果对比
- 当
StartDisabled = no
时,能检测到testFun(1)
与testFun(2)
引起的内存泄漏,由于在调用testFun(3)
前使用VLDDisable()
关闭了 VLD,因此检测不到testFun(3)
引起的内存泄漏。 - 当
StartDisabled = yes
时,只能检测到testFun(2)
引起的内存泄漏,因为在调用testFun(1)
与testFun(3)
时 VLD 都处于关闭状态。
- StartDisabled Detector Visual Leakstartdisabled detector visual leak detector visual leak vld detector方式visual leak skipheapfreeleaks detector visual leak 源码detector visual leak detector visual leak 2015 skipcrtstartupleaks detector visual leak forceincludemodulesmd detector visual leak stackwalkmethod detector visual leak detector版本visual leak