设为首页 加入收藏

TOP

【Visual Leak Detector】配置项 SelfTest
2023-07-23 13:34:22 】 浏览:21
Tags:Visual Leak Detector SelfTest

说明

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


1. 配置文件使用说明

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

2. 设置是否开启泄漏自检

参数名SelfTest

有效赋值onoff

默认值off

功能说明:打开或关闭自检模式。每次使用 VLD 时,除了检查需检测的程序是否有内存泄漏外,它也在检查自己是否有内存泄漏。将这个选项设置为 on 时,VLD 会有意地泄漏少量内存:一块大小为 22 bytes 的内存(若为 unicode 模式,则大小为 44 bytes),填充内容为 'Memory Leak Self-Test' 这个长度为 21 的字符串。这个配置项提供了一种方法来测试 VLD 检查自身内存泄漏的能力,并验证该能力是否正常工作。这个配置项只对调试 VLD 自身有用。

2.1 测试代码

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

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

    return a.exec();
}

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

2.2 SelfTest = off 时的输出

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 SelfTest = on 时的输出

VLD 输出报告:

Visual Leak Detector read settings from: D:\Program Files (x86)\Visual Leak Detector\vld.ini
Visual Leak Detector Version 2.5.1 installed.
    Performing a memory leak self-test.
No memory leaks detected.
Visual Leak Detector is now exiting.
ERROR: Visual Leak Detector: Detected a memory leak internal to Visual Leak Detector!!
---------- Block 6 at 0x01430810: 22 bytes ----------
  Call Stack:
    vld.cpp (439): Full call stack not available.
  Data:
    4D 65 6D 6F    72 79 20 4C    65 61 6B 20    53 65 6C 66     Memory.L eak.Self
    2D 54 65 73    74 00                                         -Test... ........

Visual Leak Detector passed the memory leak self-test.

2.4 输出结果对比

  • SelfTest = off 时 VLD 没有故意泄漏内存。
  • SelfTest = on 时 VLD 故意泄漏了一块 22 bytes 的内存用于展示自检功能,同时没有影响到正常的检测功能。需注意的是,如果另一个配置项 ReportEncoding = unicode ,则这块自检内存大小将为 44 bytes
】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
上一篇【Visual Leak Detector】配置项 .. 下一篇【Visual Leak Detector】配置项 ..

最新文章

热门文章

Hot 文章

Python

C 语言

C++基础

大数据基础

linux编程基础

C/C++面试题目