Qt+MSVC+VLD内存泄漏检测

VLD(全称:Visual Leak Detector)是一款用于 Visual C++ 的免费内存泄露检测工具。与其它内存检测工具相比,它在检测到内存泄漏的同时,还具有如下特点:

	可得到内存泄漏点的调用堆栈,以及其所在文件及行号;
	可得到泄露内存的完整数据;
	可设置内存泄露报告的级别;
	提供了打包好的 lib 库,使用时无须编译源码,直接使用即可;
	源码使用 GNU 许可发布,并有详尽的文档及注释。对于想深入了解堆内存管理的读者,是一个不错的选择。

下载安装

VLD 主页:https://kinddragon.github.io/vld/

文档地址:https://github.com/KindDragon/vld/wiki

GitHub 地址:https://github.com/KindDragon/vld

其他细节请查看参考链接有了它,再也不怕 C++ 内存泄露了!学习!

Qt 如何使用

使用环境 Qt+MSVC

pro文件添加如下代码

contains(QT_ARCH, x86_64){
strLib = Win64
} else {
strLib = Win32
}
INCLUDEPATH += $$quote(C:\Program Files (x86)\Visual Leak Detector\include)
LIBS += -L$$quote(C:/Program Files (x86)/Visual Leak Detector/lib/$$strLib) -lvld

vld目录可以根据需要修改,可以直接将程序及引用文件放到项目目录下

在代码内应用vld.h

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

int main(int argc, char *argv[])
{
    QCoreApplication a(argc, argv);
    int *arr = new int[10];
//    delete []arr;
    return a.exec();
}

运行结果

Visual Leak Detector read settings from: C:\Program Files (x86)\Visual Leak Detector\vld.ini
Visual Leak Detector Version 2.5.1 installed.
14:12:26: D:\QtCodeTest\build-testvld-Desktop_Qt_5_12_9_MSVC2017_32bit-Debug\debug\testvld.exe exited with code -1073741510

14:12:31: Starting D:\QtCodeTest\build-testvld-Desktop_Qt_5_12_9_MSVC2017_32bit-Debug\debug\testvld.exe ...
Visual Leak Detector read settings from: C:\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 0x00A62A98: 40 bytes ----------
  Leak Hash: 0xBC2F5060, Count: 1, Total 40 bytes
  Call Stack (TID 18160):
    ucrtbased.dll!malloc()
    d:\agent\_work\3\s\src\vctools\crt\vcstartup\src\heap\new_array.cpp (28): testvld.exe!operator new[]() + 0x9 bytes
    d:\qtcodetest\testvld\main.cpp (7): testvld.exe!main() + 0x7 bytes
    d:\agent\_work\3\s\src\vctools\crt\vcstartup\src\startup\exe_common.inl (78): testvld.exe!invoke_main() + 0x1B bytes
    d:\agent\_work\3\s\src\vctools\crt\vcstartup\src\startup\exe_common.inl (288): testvld.exe!__scrt_common_main_seh() + 0x5 bytes
    d:\agent\_work\3\s\src\vctools\crt\vcstartup\src\startup\exe_common.inl (331): testvld.exe!__scrt_common_main()
    d:\agent\_work\3\s\src\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 CD    CD CD CD CD    CD CD CD CD    CD CD CD CD     ........ ........
    CD CD CD CD    CD CD CD CD    CD CD CD CD    CD CD CD CD     ........ ........
    CD CD CD CD    CD CD CD CD                                   ........ ........


Visual Leak Detector detected 1 memory leak (76 bytes).
Largest number used: 76 bytes.
Total allocations: 76 bytes.
Visual Leak Detector is now exiting.

d:\qtcodetest\testvld\main.cpp (7): testvld.exe!main() + 0x7 bytes

修改代码

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

int main(int argc, char *argv[])
{
    QCoreApplication a(argc, argv);
    int *arr = new int[10];
    delete []arr;
    return a.exec();
}

执行结果

Visual Leak Detector read settings from: C:\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.

参考链接:
有了它,再也不怕 C++ 内存泄露了!

上一篇:Makefile里面打印信息


下一篇:Makefile里面打印信息