设为首页 加入收藏

TOP

【Visual Leak Detector】配置项 ForceIncludeModules(一)
2023-07-23 13:34:24 】 浏览:47
Tags:Visual Leak Detector ForceIncludeModules

说明

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


1. 配置文件使用说明

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

2. 设置需要检测的第三方模块

参数名ForceIncludeModules

有效赋值:包含模块名的列表。

默认值:无。

功能说明:当需要对第三方模块(exedll)进行内存泄漏检测,但又没法在这些模块的源码中 #include "vld.h" 时,可以在等号右边列出这些模块的名称,模块之间可使用任意分隔符,甚至不分隔也可以,但为了便于阅读,通常使用空格、;, 来进行分隔。需要注意的是,这个功能开启后程序有崩溃的风险,有时候还会输出错误的内存泄漏报告。经过测试,静态库 lib 不需使用此配置项,也能检测到项目 lib 库中的内存泄漏。

2.1 测试代码

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

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

    TestDll1 aaa;
    aaa.testFun(1);

    TestDll2 bbb;
    bbb.testFun(2);

    return a.exec();
}

测试环境:QT 5.9.2MSVC 2015 32bitDebug 模式,VLD 版本为 2.5.1,VLD 配置文件只对该参数做修改,测试工程所在路径为:E:\Cworkspace\Qt 5.9\QtDemo\testVLD。其中的 testdll1testdll2 是为了测试而打包的库,两个库里的函数都存在内存泄漏。

2.2 ForceIncludeModules 为空时的输出

标准输出窗显示:

testDLL1 ptr = 00840bc0, *ptr = 00000001.
testDLL2 ptr = 00840b90, *ptr = 00000002.

VLD 输出报告:

Visual Leak Detector read settings from: D:\Program Files (x86)\Visual Leak Detector\vld.ini
Visual Leak Detector Version 2.5.1 installed.
No memory leaks detected.
Visual Leak Detector is now exiting.

2.3 ForceIncludeModules = 指定库时的输出

以下 ForceIncludeModules 配置的效果相同:

ForceIncludeModules = testDll1.dll;testDll2.dll         // 使用英文分号做分隔符
ForceIncludeModules = testDll1.dll,testDll2.dll         // 使用英文逗号做分隔符
ForceIncludeModules = testDll1.dll testDll2.dll         // 使用空格做分隔符
ForceIncludeModules = testDll1.dll/testDll2.dll         // 使用斜杠做分隔符
ForceIncludeModules = testDll1.dll-testDll2.dll         // 使用减号做分隔符
ForceIncludeModules = testDll1.dlltestDll2.dll          // 不使用任何分隔符
ForceIncludeModules = testDll1.dllabcdefghtestDll2.dll  // 使用字符串abcdefgh做分隔符
ForceIncludeModules = testDll1.dll++++++testDll2.dll    // 使用字符串++++++做分隔符

标准输出窗显示:

testDLL1 ptr = 00e070f8, *ptr = 00000001.
testDLL2 ptr = 00e072a8, *ptr = 00000002.

VLD 输出报告:

Visual Leak Detector read settings from: D:\Program Files (x86)\Visual Leak Detector\vld.ini
Visual Leak Detector Version 2.5.1 installed.
    Forcing inclusion of these modules in leak detection: testdll1.dll;testdll2.dll
WARNING: Visual Leak Detector detected memory leaks!
---------- Block 1 at 0x00E070F8: 4 bytes ----------
  Leak Hash: 0x8BB01C20, Count: 1, Total 4 bytes
  Call Stack (TID 14012):
    ucrtbased.dll!malloc()
    f:\dd\vctools\crt\vcstartup\src\heap\new_scalar.cpp (19): testDll1.dll!operator new() + 0x9 bytes
    e:\cworkspace\qt 5.9\qtdemo\testdll1\testdll1.cpp (10): testDll1.dll!TestDll1::testFun() + 0x7 bytes
    e:\cworkspace\qt 5.9\qtdemo\testvld\main.cpp (11): testVLD.exe!main() + 0xB 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_commo
首页 上一页 1 2 下一页 尾页 1/2/2
】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
上一篇关于自定义Base64编解码的实现 下一篇第一部分:介绍 Spdlog 日志库

最新文章

热门文章

Hot 文章

Python

C 语言

C++基础

大数据基础

linux编程基础

C/C++面试题目