说明

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


1. 配置文件使用说明

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

2. 设置是否显示重复的泄漏块

参数名AggregateDuplicates

有效赋值yesno

默认值no

功能说明:为 yes 时将合并显示具有相同大小和相同调用堆栈的泄漏内存块,只显示首个泄漏内存块的详细信息,并显示其重复的次数;为 no 时则每次泄漏都会单独显示。

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);

    for (int i = 1; i < 3; i++)
    {
        testFun(i);
    }

    return a.exec();
}

测试环境:QT 5.9.2MSVC 2015 32bitDebug 模式,VLD 版本为 2.5.1,VLD 配置文件只对该参数做修改,测试工程所在路径为:E:CworkspaceQt 5.9QtDemotestVLD

2.2 AggregateDuplicates = no 时的输出

标准输出窗显示:

ptr = 007f7c98, *ptr = 00000001.
ptr = 007f8058, *ptr = 00000002.

VLD 输出报告:

Visual Leak Detector read settings from: D:Program Files (x86)Visual Leak Detectorvld.ini
Visual Leak Detector Version 2.5.1 installed.
WARNING: Visual Leak Detector detected memory leaks!
---------- Block 1 at 0x007F7C98: 4 bytes ----------
  Leak Hash: 0xF253A4C4, Count: 1, Total 4 bytes
  Call Stack (TID 30620):
    ucrtbased.dll!malloc()
    f:ddvctoolscrtvcstartupsrcheapnew_scalar.cpp (19): testVLD.exe!operator new() + 0x9 bytes
    e:cworkspaceqt 5.9qtdemotestvldmain.cpp (6): testVLD.exe!testFun() + 0x7 bytes
    e:cworkspaceqt 5.9qtdemotestvldmain.cpp (16): testVLD.exe!main() + 0x9 bytes
    f:ddvctoolscrtvcstartupsrcstartupexe_common.inl (74): testVLD.exe!invoke_main() + 0x1B bytes
    f:ddvctoolscrtvcstartupsrcstartupexe_common.inl (264): testVLD.exe!__scrt_common_main_seh() + 0x5 bytes
    f:ddvctoolscrtvcstartupsrcstartupexe_common.inl (309): testVLD.exe!__scrt_common_main()
    f:ddvctoolscrtvcstartupsrcstartupexe_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 0x007F8058: 4 bytes ----------
  Leak Hash: 0xF253A4C4, Count: 1, Total 4 bytes
  Call Stack (TID 30620):
    ucrtbased.dll!malloc()
    f:ddvctoolscrtvcstartupsrcheapnew_scalar.cpp (19): testVLD.exe!operator new() + 0x9 bytes
    e:cworkspaceqt 5.9qtdemotestvldmain.cpp (6): testVLD.exe!testFun() + 0x7 bytes
    e:cworkspaceqt 5.9qtdemotestvldmain.cpp (16): testVLD.exe!main() + 0x9 bytes
    f:ddvctoolscrtvcstartupsrcstartupexe_common.inl (74): testVLD.exe!invoke_main() + 0x1B bytes
    f:ddvctoolscrtvcstartupsrcstartupexe_common.inl (264): testVLD.exe!__scrt_common_main_seh() + 0x5 bytes
    f:ddvctoolscrtvcstartupsrcstartupexe_common.inl (309): testVLD.exe!__scrt_common_main()
    f:ddvctoolscrtvcstartupsrcstartupexe_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 AggregateDuplicates = yes 时的输出

标准输出窗显示:

ptr = 00a673d0, *ptr = 00000001.
ptr = 00a67400, *ptr = 00000002.

VLD 输出报告:

Visual Leak Detector read settings from: D:Program Files (x86)Visual Leak Detectorvld.ini
Visual Leak Detector Version 2.5.1 installed.
    Aggregating duplicate leaks.
WARNING: Visual Leak Detector detected memory leaks!
---------- Block 1 at 0x00A673D0: 4 bytes ----------
  Leak Hash: 0xF253A4C4, Count: 2, Total 8 bytes
  Call Stack:
    ucrtbased.dll!malloc()
    f:ddvctoolscrtvcstartupsrcheapnew_scalar.cpp (19): testVLD.exe!operator new() + 0x9 bytes
    e:cworkspaceqt 5.9qtdemotestvldmain.cpp (6): testVLD.exe!testFun() + 0x7 bytes
    e:cworkspaceqt 5.9qtdemotestvldmain.cpp (16): testVLD.exe!main() + 0x9 bytes
    f:ddvctoolscrtvcstartupsrcstartupexe_common.inl (74): testVLD.exe!invoke_main() + 0x1B bytes
    f:ddvctoolscrtvcstartupsrcstartupexe_common.inl (264): testVLD.exe!__scrt_common_main_seh() + 0x5 bytes
    f:ddvctoolscrtvcstartupsrcstartupexe_common.inl (309): testVLD.exe!__scrt_common_main()
    f:ddvctoolscrtvcstartupsrcstartupexe_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 2 memory leaks (80 bytes).
Largest number used: 80 bytes.
Total allocations: 80 bytes.
Visual Leak Detector is now exiting.

2.4 输出结果对比

  • AggregateDuplicates = yes 时合并显示了具有相同大小和相同调用堆栈的泄漏内存块,且只显示首个泄漏内存块 0x00A673D0 的详细信息,并显示其重复的次数及泄漏总大小 Count: 2, Total 8 bytes
  • AggregateDuplicates = no 时每次泄漏都会单独显示,因此 Count 恒为 1,仔细观察可以发现,对于具有相同大小和相同调用堆栈的泄漏内存块,它们的 Leak Hash 值也相同,在测试例中都为 0xF253A4C4,所以不妨推断 Leak Hash 的计算由泄漏块大小及其调用堆栈决定
内容来源于网络如有侵权请私信删除

文章来源: 博客园

原文链接: https://www.cnblogs.com/young520/p/17261378.html

你还没有登录,请先登录注册
  • 还没有人评论,欢迎说说您的想法!

相关课程