说明
使用 VLD 内存泄漏检测工具辅助开发时整理的学习笔记。本篇介绍 VLD 配置文件中配置项 TraceInternalFrames 的使用方法。同系列文章目录可见 《内存泄漏检测工具》目录
目录
1. 配置文件使用说明
在程序中通过 #include "vld.h" 的方式检测内存泄漏时,VLD 首先会尝试在程序的生成目录下读取 vld.ini 文件,若未读取成功,则会尝试在 VLD 的安装目录下读取 vld.ini 文件,若仍未读取成功,则会使用内置的默认配置,内置的默认配置如果不动源码是无法更改的,因此通过修改相应目录下的 vld.ini 文件来定制 VLD 功能是最好的选择。当配置参数等号右边为空,或者给配置了不合法值时,在使用过程中会被程序重置到默认值。
2. 设置是否跟踪内部堆栈的调用
参数名:TraceInternalFrames。
有效赋值:yes,no。
默认值:no。
功能说明:设置是否跟踪内部堆栈的调用。包含 Visual Leak Detector 的内部调用堆栈、C/C++ 及 Win32 API 的内部调用堆栈,这些内部堆栈的调用信息对检测内存泄漏是没有帮助的,默认会在堆栈跟踪期间跳过这些信息,这在一定程度上减少了检测所花费的时间。但包含 Visual Leak Detector 的内部调用堆栈,对于调试 VLD 自身是十分有用的。
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);
- 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 TraceInternalFrames = no 时的输出
标准输出窗显示:- ptr = 010bbda0, *ptr = 00000001.
复制代码 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 0x010BBDA0: 4 bytes ----------
- Leak Hash: 0x80192F03, Count: 1, Total 4 bytes
- Call Stack (TID 23280):
- 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 ........ ........
- 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.3 TraceInternalFrames = yes 时的输出
标准输出窗显示:- ptr = 01247be0, *ptr = 00000001.
复制代码 VLD 输出报告:- Visual Leak Detector read settings from: D:\Program Files (x86)\Visual Leak Detector\vld.ini
- Visual Leak Detector Version 2.5.1 installed.
- Including heap and VLD internal frames in stack traces.
- WARNING: Visual Leak Detector detected memory leaks!
- ---------- Block 1 at 0x01247BE0: 4 bytes ----------
- Leak Hash: 0x80192F03, Count: 1, Total 4 bytes
- Call Stack (TID 28424):
- ntdll.dll!RtlAllocateHeap()
- 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 ........ ........
- 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 输出结果对比
- 当 TraceInternalFrames = no 时,调用堆栈顶部是 ucrtbased.dll!malloc()。
- 当 TraceInternalFrames = yes 时,调用堆栈顶部是 ntdll.dll!RtlAllocateHeap()。
免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作! |