【Visual Leak Detector】配置项 SkipHeapFreeLeaks

打印 上一主题 下一主题

主题 844|帖子 844|积分 2542

说明

使用 VLD 内存泄漏检测工具辅助开发时整理的学习笔记。本篇介绍 VLD 配置文件中配置项 SkipHeapFreeLeaks 的使用方法。同系列文章目录可见 《内存泄漏检测工具》目录

目录


1. 配置文件使用说明

在程序中通过 #include "vld.h" 的方式检测内存泄漏时,VLD 首先会尝试在程序的生成目录下读取 vld.ini 文件,若未读取成功,则会尝试在 VLD 的安装目录下读取 vld.ini 文件,若仍未读取成功,则会使用内置的默认配置,内置的默认配置如果不动源码是无法更改的,因此通过修改相应目录下的 vld.ini 文件来定制 VLD 功能是最好的选择。当配置参数等号右边为空,或者给配置了不合法值时,在使用过程中会被程序重置到默认值。
2. 设置是否跳过堆内存泄漏检测

参数名:SkipHeapFreeLeaks。
有效赋值:yes,no。
默认值:no。
功能说明文档原话 是 Determines whether or not report memory leaks when missing HeapFree calls. 但是当我把这个变量设置为 yes 时仍可以检测到堆上的内存泄漏,这个配置项的实际作用有待进一步研究。
2.1 测试代码
  1. #include <QCoreApplication>
  2. #include "Windows.h"
  3. #include "vld.h"
  4. void testFun(int i)
  5. {
  6.     int *ptr = new int(i);
  7.     printf("ptr = %08x, *ptr = %08x.\n", ptr, *ptr);
  8.     int *pData= (int *)HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(int));
  9.     printf("pData = %08x, *pData = %08x.\n", pData, *pData);
  10. }
  11. int main(int argc, char *argv[])
  12. {
  13.     QCoreApplication a(argc, argv);
  14.     testFun(1);
  15.     return a.exec();
  16. }
复制代码
测试环境:QT 5.9.2MSVC 2015 32bitDebug 模式,VLD 版本为 2.5.1,VLD 配置文件只对该参数做修改,测试工程所在路径为:E:\Cworkspace\Qt 5.9\QtDemo\testVLD。
2.2 SkipHeapFreeLeaks = no 时的输出

标准输出窗显示:
  1. ptr = 00539498, *ptr = 00000001.
  2. pData = 00535720, *pData = 00000000.
复制代码
VLD 输出报告:
  1. Visual Leak Detector read settings from: D:\Program Files (x86)\Visual Leak Detector\vld.ini
  2. Visual Leak Detector Version 2.5.1 installed.
  3. WARNING: Visual Leak Detector detected memory leaks!
  4. ---------- Block 2 at 0x00535720: 4 bytes ----------
  5.   Leak Hash: 0x426C3DE0, Count: 1, Total 4 bytes
  6.   Call Stack (TID 30052):
  7.     ntdll.dll!RtlAllocateHeap()
  8.     e:\cworkspace\qt 5.9\qtdemo\testvld\main.cpp (10): testVLD.exe!testFun() + 0x11 bytes
  9.     e:\cworkspace\qt 5.9\qtdemo\testvld\main.cpp (18): testVLD.exe!main() + 0x7 bytes
  10.     f:\dd\vctools\crt\vcstartup\src\startup\exe_common.inl (74): testVLD.exe!invoke_main() + 0x1B bytes
  11.     f:\dd\vctools\crt\vcstartup\src\startup\exe_common.inl (264): testVLD.exe!__scrt_common_main_seh() + 0x5 bytes
  12.     f:\dd\vctools\crt\vcstartup\src\startup\exe_common.inl (309): testVLD.exe!__scrt_common_main()
  13.     f:\dd\vctools\crt\vcstartup\src\startup\exe_main.cpp (17): testVLD.exe!mainCRTStartup()
  14.     KERNEL32.DLL!BaseThreadInitThunk() + 0x19 bytes
  15.     ntdll.dll!RtlGetAppContainerNamedObjectPath() + 0x11E bytes
  16.     ntdll.dll!RtlGetAppContainerNamedObjectPath() + 0xEE bytes
  17.   Data:
  18.     00 00 00 00                                                  ........ ........
  19. ---------- Block 1 at 0x00539498: 4 bytes ----------
  20.   Leak Hash: 0x19BB156F, Count: 1, Total 4 bytes
  21.   Call Stack (TID 30052):
  22.     ucrtbased.dll!malloc()
  23.     f:\dd\vctools\crt\vcstartup\src\heap\new_scalar.cpp (19): testVLD.exe!operator new() + 0x9 bytes
  24.     e:\cworkspace\qt 5.9\qtdemo\testvld\main.cpp (7): testVLD.exe!testFun() + 0x7 bytes
  25.     e:\cworkspace\qt 5.9\qtdemo\testvld\main.cpp (18): testVLD.exe!main() + 0x7 bytes
  26.     f:\dd\vctools\crt\vcstartup\src\startup\exe_common.inl (74): testVLD.exe!invoke_main() + 0x1B bytes
  27.     f:\dd\vctools\crt\vcstartup\src\startup\exe_common.inl (264): testVLD.exe!__scrt_common_main_seh() + 0x5 bytes
  28.     f:\dd\vctools\crt\vcstartup\src\startup\exe_common.inl (309): testVLD.exe!__scrt_common_main()
  29.     f:\dd\vctools\crt\vcstartup\src\startup\exe_main.cpp (17): testVLD.exe!mainCRTStartup()
  30.     KERNEL32.DLL!BaseThreadInitThunk() + 0x19 bytes
  31.     ntdll.dll!RtlGetAppContainerNamedObjectPath() + 0x11E bytes
  32.     ntdll.dll!RtlGetAppContainerNamedObjectPath() + 0xEE bytes
  33.   Data:
  34.     01 00 00 00                                                  ........ ........
  35. Visual Leak Detector detected 2 memory leaks (44 bytes).
  36. Largest number used: 44 bytes.
  37. Total allocations: 44 bytes.
  38. Visual Leak Detector is now exiting.
复制代码
2.3 SkipHeapFreeLeaks = yes 时的输出

标准输出窗显示:
  1. ptr = 014e9450, *ptr = 00000001.
  2. pData = 014e84b0, *pData = 00000000.
复制代码
VLD 输出报告:
  1. Visual Leak Detector read settings from: D:\Program Files (x86)\Visual Leak Detector\vld.ini
  2. Visual Leak Detector Version 2.5.1 installed.
  3. WARNING: Visual Leak Detector detected memory leaks!
  4. ---------- Block 2 at 0x014E84B0: 4 bytes ----------
  5.   Leak Hash: 0x7C37FF93, Count: 1, Total 4 bytes
  6.   Call Stack (TID 7564):
  7.     ntdll.dll!RtlAllocateHeap()
  8.     e:\cworkspace\qt 5.9\qtdemo\testvld\main.cpp (10): testVLD.exe!testFun() + 0x11 bytes
  9.     e:\cworkspace\qt 5.9\qtdemo\testvld\main.cpp (18): testVLD.exe!main() + 0x7 bytes
  10.     f:\dd\vctools\crt\vcstartup\src\startup\exe_common.inl (74): testVLD.exe!invoke_main() + 0x1B bytes
  11.     f:\dd\vctools\crt\vcstartup\src\startup\exe_common.inl (264): testVLD.exe!__scrt_common_main_seh() + 0x5 bytes
  12.     f:\dd\vctools\crt\vcstartup\src\startup\exe_common.inl (309): testVLD.exe!__scrt_common_main()
  13.     f:\dd\vctools\crt\vcstartup\src\startup\exe_main.cpp (17): testVLD.exe!mainCRTStartup()
  14.     KERNEL32.DLL!BaseThreadInitThunk() + 0x19 bytes
  15.     ntdll.dll!RtlGetAppContainerNamedObjectPath() + 0x11E bytes
  16.     ntdll.dll!RtlGetAppContainerNamedObjectPath() + 0xEE bytes
  17.   Data:
  18.     00 00 00 00                                                  ........ ........
  19. ---------- Block 1 at 0x014E9450: 4 bytes ----------
  20.   Leak Hash: 0x8876C556, Count: 1, Total 4 bytes
  21.   Call Stack (TID 7564):
  22.     ucrtbased.dll!malloc()
  23.     f:\dd\vctools\crt\vcstartup\src\heap\new_scalar.cpp (19): testVLD.exe!operator new() + 0x9 bytes
  24.     e:\cworkspace\qt 5.9\qtdemo\testvld\main.cpp (7): testVLD.exe!testFun() + 0x7 bytes
  25.     e:\cworkspace\qt 5.9\qtdemo\testvld\main.cpp (18): testVLD.exe!main() + 0x7 bytes
  26.     f:\dd\vctools\crt\vcstartup\src\startup\exe_common.inl (74): testVLD.exe!invoke_main() + 0x1B bytes
  27.     f:\dd\vctools\crt\vcstartup\src\startup\exe_common.inl (264): testVLD.exe!__scrt_common_main_seh() + 0x5 bytes
  28.     f:\dd\vctools\crt\vcstartup\src\startup\exe_common.inl (309): testVLD.exe!__scrt_common_main()
  29.     f:\dd\vctools\crt\vcstartup\src\startup\exe_main.cpp (17): testVLD.exe!mainCRTStartup()
  30.     KERNEL32.DLL!BaseThreadInitThunk() + 0x19 bytes
  31.     ntdll.dll!RtlGetAppContainerNamedObjectPath() + 0x11E bytes
  32.     ntdll.dll!RtlGetAppContainerNamedObjectPath() + 0xEE bytes
  33.   Data:
  34.     01 00 00 00                                                  ........ ........
  35. Visual Leak Detector detected 2 memory leaks (44 bytes).
  36. Largest number used: 44 bytes.
  37. Total allocations: 44 bytes.
  38. Visual Leak Detector is now exiting.
复制代码
2.4 输出结果对比

SkipHeapFreeLeaks = no 与 SkipHeapFreeLeaks = yes 的输出结果似乎没有什么实质性的差异,不过有一点需注意,使用 new 分配堆内存时,会额外添加 36 bytes 大小的内存用于追踪管理,但使用 HeapAlloc() 函数分配堆内存时,则没有额外的内存使用,因此上述测试例的 Total allocations 为 44 bytes,而不是 80 bytes。

免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!
回复

使用道具 举报

0 个回复

倒序浏览

快速回复

您需要登录后才可以回帖 登录 or 立即注册

本版积分规则

来自云龙湖轮廓分明的月亮

金牌会员
这个人很懒什么都没写!
快速回复 返回顶部 返回列表