【Visual Leak Detector】配置项 MaxTraceFrames

打印 上一主题 下一主题

主题 545|帖子 545|积分 1635

说明

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

目录


1. 配置文件使用说明

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

参数名:MaxTraceFrames。
有效赋值:1~4294967295。
默认值:64。
功能说明:泄漏检测期间要回溯的最大调用堆栈帧数。将其设置在一个较低的数字可以减少内存泄漏检测造成的 CPU 占用开销,特别是当 StackWalkMethod = safe 的时候。需要注意的是,这个计数值与输出中显示的调用堆栈帧条数不是完全一致的,但总体趋势一致,即这个数值越大显示的调用堆栈帧条数越多。
2.1 测试代码
  1. #include <QCoreApplication>
  2. #include "vld.h"
  3. void testFun(int i)
  4. {
  5.     if (i > 1)
  6.     {
  7.         testFun(i-1);
  8.     }else
  9.     {
  10.         int *ptr = new int(i);
  11.         printf("ptr = %08x, *ptr = %08x.\n", ptr, *ptr);
  12.     }
  13. }
  14. int main(int argc, char *argv[])
  15. {
  16.     QCoreApplication a(argc, argv);
  17.     testFun(10);
  18.     return a.exec();
  19. }
复制代码
测试环境:QT 5.9.2MSVC 2015 32bitDebug 模式,VLD 版本为 2.5.1,VLD 配置文件只对该参数做修改,测试工程所在路径为:E:\Cworkspace\Qt 5.9\QtDemo\testVLD。
2.2 MaxTraceFrames 为空时的输出

标准输出窗显示:
  1. ptr = 0117b200, *ptr = 00000001.
复制代码
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 1 at 0x0117B200: 4 bytes ----------
  5.   Leak Hash: 0x1816EFF6, Count: 1, Total 4 bytes
  6.   Call Stack (TID 1828):
  7.     ucrtbased.dll!malloc()
  8.     f:\dd\vctools\crt\vcstartup\src\heap\new_scalar.cpp (19): testVLD.exe!operator new() + 0x9 bytes
  9.     e:\cworkspace\qt 5.9\qtdemo\testvld\main.cpp (11): testVLD.exe!testFun() + 0x7 bytes
  10.     e:\cworkspace\qt 5.9\qtdemo\testvld\main.cpp (8): testVLD.exe!testFun() + 0xC bytes
  11.     e:\cworkspace\qt 5.9\qtdemo\testvld\main.cpp (8): testVLD.exe!testFun() + 0xC bytes
  12.     e:\cworkspace\qt 5.9\qtdemo\testvld\main.cpp (8): testVLD.exe!testFun() + 0xC bytes
  13.     e:\cworkspace\qt 5.9\qtdemo\testvld\main.cpp (8): testVLD.exe!testFun() + 0xC bytes
  14.     e:\cworkspace\qt 5.9\qtdemo\testvld\main.cpp (8): testVLD.exe!testFun() + 0xC bytes
  15.     e:\cworkspace\qt 5.9\qtdemo\testvld\main.cpp (8): testVLD.exe!testFun() + 0xC bytes
  16.     e:\cworkspace\qt 5.9\qtdemo\testvld\main.cpp (8): testVLD.exe!testFun() + 0xC bytes
  17.     e:\cworkspace\qt 5.9\qtdemo\testvld\main.cpp (8): testVLD.exe!testFun() + 0xC bytes
  18.     e:\cworkspace\qt 5.9\qtdemo\testvld\main.cpp (8): testVLD.exe!testFun() + 0xC bytes
  19.     e:\cworkspace\qt 5.9\qtdemo\testvld\main.cpp (20): testVLD.exe!main() + 0x7 bytes
  20.     f:\dd\vctools\crt\vcstartup\src\startup\exe_common.inl (74): testVLD.exe!invoke_main() + 0x1B bytes
  21.     f:\dd\vctools\crt\vcstartup\src\startup\exe_common.inl (264): testVLD.exe!__scrt_common_main_seh() + 0x5 bytes
  22.     f:\dd\vctools\crt\vcstartup\src\startup\exe_common.inl (309): testVLD.exe!__scrt_common_main()
  23.     f:\dd\vctools\crt\vcstartup\src\startup\exe_main.cpp (17): testVLD.exe!mainCRTStartup()
  24.     KERNEL32.DLL!BaseThreadInitThunk() + 0x19 bytes
  25.     ntdll.dll!RtlGetAppContainerNamedObjectPath() + 0x11E bytes
  26.     ntdll.dll!RtlGetAppContainerNamedObjectPath() + 0xEE bytes
  27.   Data:
  28.     01 00 00 00                                                  ........ ........
  29. Visual Leak Detector detected 1 memory leak (40 bytes).
  30. Largest number used: 40 bytes.
  31. Total allocations: 40 bytes.
  32. Visual Leak Detector is now exiting.
复制代码
2.3 MaxTraceFrames = 5 时的输出

标准输出窗显示:
  1. ptr = 00a0c678, *ptr = 00000001.
复制代码
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.     Limiting stack traces to 5 frames.
  4. WARNING: Visual Leak Detector detected memory leaks!
  5. ---------- Block 1 at 0x00A0C678: 4 bytes ----------
  6.   Leak Hash: 0x8483CF43, Count: 1, Total 4 bytes
  7.   Call Stack (TID 32984):
  8.     ucrtbased.dll!malloc()
  9.     f:\dd\vctools\crt\vcstartup\src\heap\new_scalar.cpp (19): testVLD.exe!operator new() + 0x9 bytes
  10.     e:\cworkspace\qt 5.9\qtdemo\testvld\main.cpp (11): testVLD.exe!testFun() + 0x7 bytes
  11.     e:\cworkspace\qt 5.9\qtdemo\testvld\main.cpp (8): testVLD.exe!testFun() + 0xC bytes
  12.     e:\cworkspace\qt 5.9\qtdemo\testvld\main.cpp (8): testVLD.exe!testFun() + 0xC bytes
  13.     e:\cworkspace\qt 5.9\qtdemo\testvld\main.cpp (8): testVLD.exe!testFun() + 0xC bytes
  14.     e:\cworkspace\qt 5.9\qtdemo\testvld\main.cpp (8): testVLD.exe!testFun() + 0xC bytes
  15.     e:\cworkspace\qt 5.9\qtdemo\testvld\main.cpp (8): testVLD.exe!testFun() + 0xC bytes
  16.     e:\cworkspace\qt 5.9\qtdemo\testvld\main.cpp (8): testVLD.exe!testFun() + 0xC bytes
  17.     e:\cworkspace\qt 5.9\qtdemo\testvld\main.cpp (8): testVLD.exe!testFun() + 0xC bytes
  18.     e:\cworkspace\qt 5.9\qtdemo\testvld\main.cpp (8): testVLD.exe!testFun() + 0xC bytes
  19.     e:\cworkspace\qt 5.9\qtdemo\testvld\main.cpp (8): testVLD.exe!testFun() + 0xC bytes
  20.     e:\cworkspace\qt 5.9\qtdemo\testvld\main.cpp (20): testVLD.exe!main() + 0x7 bytes
  21.   Data:
  22.     01 00 00 00                                                  ........ ........
  23. Visual Leak Detector detected 1 memory leak (40 bytes).
  24. Largest number used: 40 bytes.
  25. Total allocations: 40 bytes.
  26. Visual Leak Detector is now exiting.
复制代码
2.5 输出结果对比


  • 当 MaxTraceFrames 为空时,使用默认值 64,输出中显示了全部的历史调用堆栈。
  • 当 MaxTraceFrames = 5 时,输出中显示的历史调用堆栈条数变少了。

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

使用道具 举报

0 个回复

倒序浏览

快速回复

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

本版积分规则

火影

金牌会员
这个人很懒什么都没写!

标签云

快速回复 返回顶部 返回列表