设为首页 加入收藏

TOP

【Visual Leak Detector】配置项 StartDisabled(一)
2023-07-23 13:34:18 】 浏览:51
Tags:Visual Leak Detector StartDisabled

说明

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


1. 配置文件使用说明

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

2. 设置是否禁用自动初始化

参数名StartDisabled

有效赋值yesno

默认值no

功能说明:设置是否禁用自动初始化,当 StartDisabled = yes 时,将会禁用 VLD 的自动初始化,此时若想检测程序中的某个函数、某个线程是否存在内存泄漏(即在运行过程中控制 VLD 的开启与关闭),需提前人为地调用 VLDEnable() 函数来进行初始化,当被测试的函数结束后,还可以使用 VLDDisable() 函数来关闭 VLD 内存检测。需注意的是,使用这种人为的控制方法时,检测不到 VLD 关闭期间的内存分配引起的内存泄漏;此外,调用 VLDEnable()VLDDisable() 函数时,仅仅对调用函数的线程有效,对于同一进程的其他的线程无效,除非在每个线程中都进行了调用。详见 Controlling-Leak-Detection-at-Runtime

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

    testFun(1);

    VLDEnable();
    testFun(2);
    VLDDisable();

    testFun(3);

    return a.exec();
}

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

2.2 StartDisabled = no 时的输出

标准输出窗显示:

ptr = 00585f68, *ptr = 00000001.
ptr = 00586088, *ptr = 00000002.
ptr = 005860e8, *ptr = 00000003.

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 0x00585F68: 4 bytes ----------
  Leak Hash: 0xA089A5DA, Count: 1, Total 4 bytes
  Call Stack (TID 33388):
    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!testFun() + 0x7 bytes
    e:\cworkspace\qt 5.9\qtdemo\testvld\main.cpp (14): testVLD.exe!main() + 0x7 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_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:
    01 00 00 00                                                  ........ ........


---------- Block 2 at 0x00586088: 4 bytes ----------
  Leak Hash: 0xF090F245, Count: 1, Total 4 bytes
  Call Stack (TID 33388):
    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!testFun() + 0x7 bytes
    e:\cworkspace\qt 5.9\qtdemo\testvld\main.cpp (17): testVLD.exe!main() + 0x7 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
首页 上一页 1 2 下一页 尾页 1/2/2
】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
上一篇Visual Studio C++ 默认 UTF-8 编.. 下一篇recompile with -fPIC

最新文章

热门文章

Hot 文章

Python

C 语言

C++基础

大数据基础

linux编程基础

C/C++面试题目