草庐IT

【Visual Leak Detector】QT 中 VLD 输出解析(三)

木三百川 2023-03-28 原文

说明

使用 VLD 内存泄漏检测工具辅助开发时整理的学习笔记。


1. 使用方式

在 QT 中使用 VLD 的方法可以查看另外几篇博客:

本次测试使用的环境为:QT 5.9.2MSVC 2015 32bitDebug 模式,VLD 版本为 2.5.1,VLD 配置文件不做任何更改使用默认配置,测试工程所在路径为:E:\Cworkspace\Qt 5.9\QtDemo\testVLD

2. 有三处内存泄漏时的输出报告

写一个有三处内存泄漏的程序,如下:

#include <QCoreApplication>
#include "vld.h"

void testFun1()
{
    int *ptr = new int(0x55345678);
    printf("ptr1 = %08x, *ptr1 = %08x.\n", ptr, *ptr);
}

void testFun2()
{
    short *ptr = new short(0x4529);
    printf("ptr2 = %08x, *ptr2 = %04x.\n", ptr, *ptr);
}

void testFun3()
{
    char *ptr = new char[3];
    printf("ptr3 = %08x.\n", ptr, *ptr);
}

int main(int argc, char *argv[])
{
    QCoreApplication a(argc, argv);

    testFun1();
    testFun2();
    testFun3();

    return a.exec();
}

程序运行时,在标准输出窗会输出以下结果:

ptr1 = 00b674b8, *ptr1 = 55345678.
ptr2 = 00b670f8, *ptr2 = 4529.
ptr3 = 00b674e8.

程序运行结束后,检测到了内存泄漏,VLD 会输出以下报告(本例中出现三处内存泄漏),第 1~3 行显示 VLD 运行状态,第 4~19 行显示 testFun2() 函数泄漏内存的详细信息,第 22~37 行显示 testFun1() 函数泄漏内存的详细信息,第 40~55 行显示 testFun3() 函数泄漏内存的详细信息,第 58~60 行总结此次泄漏情况,第 61 行显示 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 2 at 0x00B670F8: 2 bytes ----------
  Leak Hash: 0xB9FC7D06, Count: 1, Total 2 bytes
  Call Stack (TID 14904):
    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 (12): testVLD.exe!testFun2() + 0x7 bytes
    e:\cworkspace\qt 5.9\qtdemo\testvld\main.cpp (28): 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:
    29 45                                                        )E...... ........


---------- Block 1 at 0x00B674B8: 4 bytes ----------
  Leak Hash: 0xE622CBED, Count: 1, Total 4 bytes
  Call Stack (TID 14904):
    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!testFun1() + 0x7 bytes
    e:\cworkspace\qt 5.9\qtdemo\testvld\main.cpp (27): 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.... ........


---------- Block 3 at 0x00B674E8: 3 bytes ----------
  Leak Hash: 0x1ED7DC7D, Count: 1, Total 3 bytes
  Call Stack (TID 14904):
    ucrtbased.dll!malloc()
    f:\dd\vctools\crt\vcstartup\src\heap\new_array.cpp (15): testVLD.exe!operator new[]() + 0x9 bytes
    e:\cworkspace\qt 5.9\qtdemo\testvld\main.cpp (18): testVLD.exe!testFun3() + 0x7 bytes
    e:\cworkspace\qt 5.9\qtdemo\testvld\main.cpp (30): 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:
    CD CD CD                                                     ........ ........


Visual Leak Detector detected 3 memory leaks (117 bytes).
Largest number used: 117 bytes.
Total allocations: 117 bytes.
Visual Leak Detector is now exiting.

观察以上输出报告,可以总结出以下规律:

  • Block 后面的序号和函数调用顺序有关,先调用的序号小,在主函数中 testFun1()testFun2()testFun3() 是依次调用的,分别对应于 Block 1Block 2Block 3
  • 报告输出时泄漏信息的显示顺序和地址大小有关,地址小的先显示Block 1Block 2Block 3 的泄漏首地址分别为 0x00B674B80x00B670F80x00B674E8,内存地址从小到大排序为 Block 2Block 1Block 3,显示顺序正是如此。

每个 Block 输出的详细解析可以查看另外一篇博客 【Visual Leak Detector】QT 中 VLD 输出解析(二)。第 58~60 行中的 117 bytes 包含有: Block 1 中申请 int4 bytes 及对应的 36 bytes 内存管理头、 Block 2 中申请 short2 bytes 及对应的 36 bytes 内存管理头、 Block 3 中申请 char[3]3 bytes 及对应的 36 bytes 内存管理头,共计 \(4 + 36 + 2 + 36 + 3 + 36 = 117 bytes\)

3. 有两处内存泄漏时的输出报告

testFun2() 函数中申请的内存正常释放,测试代码如下:

#include <QCoreApplication>
#include "vld.h"

void testFun1()
{
    int *ptr = new int(0x55345678);
    printf("ptr1 = %08x, *ptr1 = %08x.\n", ptr, *ptr);
}

void testFun2()
{
    short *ptr = new short(0x4529);
    printf("ptr2 = %08x, *ptr2 = %04x.\n", ptr, *ptr);
    delete ptr;
}

void testFun3()
{
    char *ptr = new char[3];
    printf("ptr3 = %08x.\n", ptr, *ptr);
}

int main(int argc, char *argv[])
{
    QCoreApplication a(argc, argv);

    testFun1();
    testFun2();
    testFun3();

    return a.exec();
}

程序运行时,在标准输出窗会输出以下结果:

ptr1 = 006c5c68, *ptr1 = 55345678.
ptr2 = 006c5cc8, *ptr2 = 4529.
ptr3 = 006c5db8.

程序运行结束后,检测到了内存泄漏,VLD 会输出以下报告(本例中出现两处内存泄漏),第 1~3 行显示 VLD 运行状态,第 4~19 行显示 testFun1() 函数泄漏内存的详细信息,第 22~37 行显示 testFun3() 函数泄漏内存的详细信息,第 40~42 行总结此次泄漏情况,第 43 行显示 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 0x006C5C68: 4 bytes ----------
  Leak Hash: 0x1E4EE072, Count: 1, Total 4 bytes
  Call Stack (TID 11992):
    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!testFun1() + 0x7 bytes
    e:\cworkspace\qt 5.9\qtdemo\testvld\main.cpp (28): 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.... ........


---------- Block 3 at 0x006C5DB8: 3 bytes ----------
  Leak Hash: 0xE91F4A96, Count: 1, Total 3 bytes
  Call Stack (TID 11992):
    ucrtbased.dll!malloc()
    f:\dd\vctools\crt\vcstartup\src\heap\new_array.cpp (15): testVLD.exe!operator new[]() + 0x9 bytes
    e:\cworkspace\qt 5.9\qtdemo\testvld\main.cpp (19): testVLD.exe!testFun3() + 0x7 bytes
    e:\cworkspace\qt 5.9\qtdemo\testvld\main.cpp (31): 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:
    CD CD CD                                                     ........ ........


Visual Leak Detector detected 2 memory leaks (79 bytes).
Largest number used: 79 bytes.
Total allocations: 117 bytes.
Visual Leak Detector is now exiting.

观察以上输出报告,可知 Block 后面的序号不仅仅针对导致内存泄漏的函数,这个例子中的 Block 2 应该属于 testFun2(),但由于 testFun2() 正常释放了内存,因此在输出报告中没有找到 Block 2,只能找到 Block 1Block 3,这个例子说明了输出报告中 Block 序号不一定是连续的,可能缺失了一些序号,属正常现象。第 40~41 行中的 79 bytes 包含有: Block 1 中申请 int4 bytes 及对应的 36 bytes 内存管理头、 Block 3 中申请 char[3]3 bytes 及对应的 36 bytes 内存管理头,共计 \(4 + 36 + 3 + 36 = 79 bytes\)。第 42 行中的 117 bytes 表示运行过程中一共分配的内存大小,计算方式与上一节一样。

有关【Visual Leak Detector】QT 中 VLD 输出解析(三)的更多相关文章

  1. Ruby 解析字符串 - 2

    我有一个字符串input="maybe(thisis|thatwas)some((nice|ugly)(day|night)|(strange(weather|time)))"Ruby中解析该字符串的最佳方法是什么?我的意思是脚本应该能够像这样构建句子:maybethisissomeuglynightmaybethatwassomenicenightmaybethiswassomestrangetime等等,你明白了......我应该一个字符一个字符地读取字符串并构建一个带有堆栈的状态机来存储括号值以供以后计算,还是有更好的方法?也许为此目的准备了一个开箱即用的库?

  2. ruby - 解析 RDFa、微数据等的最佳方式是什么,使用统一的模式/词汇(例如 schema.org)存储和显示信息 - 2

    我主要使用Ruby来执行此操作,但到目前为止我的攻击计划如下:使用gemsrdf、rdf-rdfa和rdf-microdata或mida来解析给定任何URI的数据。我认为最好映射到像schema.org这样的统一模式,例如使用这个yaml文件,它试图描述数据词汇表和opengraph到schema.org之间的转换:#SchemaXtoschema.orgconversion#data-vocabularyDV:name:namestreet-address:streetAddressregion:addressRegionlocality:addressLocalityphoto:i

  3. ruby - 用逗号、双引号和编码解析 csv - 2

    我正在使用ruby​​1.9解析以下带有MacRoman字符的csv文件#encoding:ISO-8859-1#csv_parse.csvName,main-dialogue"Marceu","Giveittohimóhe,hiswife."我做了以下解析。require'csv'input_string=File.read("../csv_parse.rb").force_encoding("ISO-8859-1").encode("UTF-8")#=>"Name,main-dialogue\r\n\"Marceu\",\"Giveittohim\x97he,hiswife.\"\

  4. ruby - 检查 "command"的输出应该包含 NilClass 的意外崩溃 - 2

    为了将Cucumber用于命令行脚本,我按照提供的说明安装了arubagem。它在我的Gemfile中,我可以验证是否安装了正确的版本并且我已经包含了require'aruba/cucumber'在'features/env.rb'中为了确保它能正常工作,我写了以下场景:@announceScenario:Testingcucumber/arubaGivenablankslateThentheoutputfrom"ls-la"shouldcontain"drw"假设事情应该失败。它确实失败了,但失败的原因是错误的:@announceScenario:Testingcucumber/ar

  5. ruby - 通过 erb 模板输出 ruby​​ 数组 - 2

    我正在使用puppet为ruby​​程序提供一组常量。我需要提供一组主机名,我的程序将对其进行迭代。在我之前使用的bash脚本中,我只是将它作为一个puppet变量hosts=>"host1,host2"我将其提供给bash脚本作为HOSTS=显然这对ruby​​不太适用——我需要它的格式hosts=["host1","host2"]自从phosts和putsmy_array.inspect提供输出["host1","host2"]我希望使用其中之一。不幸的是,我终其一生都无法弄清楚如何让它发挥作用。我尝试了以下各项:我发现某处他们指出我需要在函数调用前放置“function_”……这

  6. ruby - 如何进行排列以有效地定制输出 - 2

    这是一道面试题,我没有答对,但还是很好奇怎么解。你有N个人的大家庭,分别是1,2,3,...,N岁。你想给你的大家庭拍张照片。所有的家庭成员都排成一排。“我是家里的friend,建议家庭成员安排如下:”1岁的家庭成员坐在这一排的最左边。每两个坐在一起的家庭成员的年龄相差不得超过2岁。输入:整数N,1≤N≤55。输出:摄影师可以拍摄的照片数量。示例->输入:4,输出:4符合条件的数组:[1,2,3,4][1,2,4,3][1,3,2,4][1,3,4,2]另一个例子:输入:5输出:6符合条件的数组:[1,2,3,4,5][1,2,3,5,4][1,2,4,3,5][1,2,4,5,3][

  7. ruby-on-rails - 我更新了 ruby​​ gems,现在到处都收到解析树错误和弃用警告! - 2

    简而言之错误:NOTE:Gem::SourceIndex#add_specisdeprecated,useSpecification.add_spec.Itwillberemovedonorafter2011-11-01.Gem::SourceIndex#add_speccalledfrom/opt/local/lib/ruby/site_ruby/1.8/rubygems/source_index.rb:91./opt/local/lib/ruby/gems/1.8/gems/rails-2.3.8/lib/rails/gem_dependency.rb:275:in`==':und

  8. ruby - 将 spawn() 的标准输出/标准错误重定向到 Ruby 中的字符串 - 2

    我想使用spawn(针对多个并发子进程)在Ruby中执行一个外部进程,并将标准输出或标准错误收集到一个字符串中,其方式类似于使用Python的子进程Popen.communicate()可以完成的操作。我尝试将:out/:err重定向到一个新的StringIO对象,但这会生成一个ArgumentError,并且临时重新定义$stdxxx会混淆子进程的输出。 最佳答案 如果你不喜欢popen,这是我的方法:r,w=IO.pipepid=Process.spawn(command,:out=>w,:err=>[:child,:out])

  9. Qt Designer的简单使用 - 2

    在前面两节的例子中,主界面窗口的尺寸和标签控件显示的矩形区域等,都是用C++代码编写的。窗口和控件的尺寸都是预估的,控件如果多起来,那就不好估计每个控件合适的位置和大小了。用C++代码编写图形界面的问题就是不直观,因此Qt项目开发了专门的可视化图形界面编辑器——QtDesigner(Qt设计师)。通过QtDesigner就可以很方便地创建图形界面文件*.ui,然后将ui文件应用到源代码里面,做到“所见即所得”,大大方便了图形界面的设计。本节就演示一下QtDesigner的简单使用,学习拖拽控件和设置控件属性,并将ui文件应用到Qt程序代码里。使用QtDesigner设计界面在开始菜单中找到「Q

  10. ruby - Ruby 是否使用 $stdout 来写入 puts 和 return 的输出? - 2

    我想知道Ruby用来在命令行打印这些东西的输出流:irb(main):001:0>a="test"=>"test"irb(main):002:0>putsatest=>nilirb(main):003:0>a=>"test"$stdout是否用于irb(main):002:0>和irb(main):003:0>?而且,在这两次调用之间,$stdout的值是否有任何变化?另外,有人能告诉我打印/写入这些内容的Ruby源代码吗? 最佳答案 是的。而且很容易向自己测试/证明。在命令行试试这个:ruby-e'puts"foo"'>test.

随机推荐