C++内存泄露检测工具(VLD)

  简述


C/C++ 程序越复杂,内存的管理显得越重要,稍有不慎就会出现泄漏。如果内存泄漏不是很严重,在短时间内对程序不会有太大影响,这也使得内存泄漏问题有很强的隐蔽性,不易被发现。然而不管内存泄漏多么轻微,当程序长时间运行时,其破坏力是惊人的 - 从性能下降到内存耗尽,甚至会影响其他程序的正常运行。

解决方法:VLD 工具

下载:Visual Leak Detector | Enhanced Memory Leak Detection for Visual C++

百度云:链接:https://pan.baidu.com/s/1TM61vZdSALuisbhDFCwfmA 
提取码:6666

使用方法:

1、点击vld-2.5.1-setup.exe安装到指定文件夹下,这边安装的是vld库的文件,不是真正的执行文件,可放置到自己熟悉的文件夹中;

2、这里我们只需要关注上图所示的三个文件夹,在bin文件夹下,我们需要如下红框内的文件,我们需要拷贝到我们编译出来的exe目录下即可;

3、 include和lib文件是需要我们链接到工程里的文件,我们需要拷贝这两个文件夹到自己的工程里,最后还在在.pro文件中加入链接标识:具体怎么链接,看你文件位置

INCLUDEPATH +=.\lib\include
LIBS += -L$$PWD\lib\ -lvld

 4、最后只需要在main.cpp中加入vld头文件即可

#include "vld.h"

 注意:VLD只在debug环境下才能启动,并且只支持msvc编译器不支持mingw;

实战:

 正常代码:

Widget::Widget(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::Widget)
{
    ui->setupUi(this);

}

 执行后,VLD工具提供的信息:

Visual Leak Detector read settings from: E:\Visual Leak Detector\vld.ini
Visual Leak Detector Version 2.5.1 installed.
No memory leaks detected.
Visual Leak Detector read settings from: E:\Visual Leak Detector\vld.ini
Visual Leak Detector Version 2.5.1 installed.
No memory leaks detected.
Visual Leak Detector is now exiting.
Visual Leak Detector is now exiting.

从提示信息中可发现最后报告说: No memory leaks detected. 没有内存泄露;

下面演示异常代码

Widget::Widget(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::Widget)
{
    ui->setupUi(this);
   int *p = new int[2];

   int *e = new int[4];


}

在构造函数中,加入了两段内存泄露的代码,我们编译下看看结果:

Visual Leak Detector read settings from: E:\Visual Leak Detector\vld.ini
Visual Leak Detector Version 2.5.1 installed.
WARNING: Visual Leak Detector detected memory leaks!
Visual Leak Detector read settings from: E:\Visual Leak Detector\vld.ini
Visual Leak Detector Version 2.5.1 installed.
WARNING: Visual Leak Detector detected memory leaks!
---------- Block 5 at 0x00FD6D20: 16 bytes ----------
  Leak Hash: 0x13CBCD78, Count: 1, Total 16 bytes
  Call Stack (TID 25088):
    ucrtbased.dll!malloc()
    f:\dd\vctools\crt\vcstartup\src\heap\new_array.cpp (15): VLD_Test.exe!operator new[]() + 0x9 bytes
    c:\users\administrator\documents\vld_test\widget.cpp (11): VLD_Test.exe!Widget::Widget() + 0x7 bytes
    c:\users\administrator\documents\vld_test\main.cpp (9): VLD_Test.exe!main() + 0xA bytes
    c:\users\qt\work\qt\qtbase\src\winmain\qtmain_win.cpp (104): VLD_Test.exe!WinMain() + 0xD bytes
    f:\dd\vctools\crt\vcstartup\src\startup\exe_common.inl (109): VLD_Test.exe!invoke_main()
    f:\dd\vctools\crt\vcstartup\src\startup\exe_common.inl (264): VLD_Test.exe!__scrt_common_main_seh() + 0x5 bytes
    f:\dd\vctools\crt\vcstartup\src\startup\exe_common.inl (309): VLD_Test.exe!__scrt_common_main()
    f:\dd\vctools\crt\vcstartup\src\startup\exe_winmain.cpp (17): VLD_Test.exe!WinMainCRTStartup()
    KERNEL32.DLL!BaseThreadInitThunk() + 0x19 bytes
    ntdll.dll!RtlGetAppContainerNamedObjectPath() + 0xED bytes
    ntdll.dll!RtlGetAppContainerNamedObjectPath() + 0xBD bytes
  Data:
    CD CD CD CD    CD CD CD CD    CD CD CD CD    CD CD CD CD     ........ ........


---------- Block 4 at 0x00FEB608: 8 bytes ----------
  Leak Hash: 0xE523555D, Count: 1, Total 8 bytes
  Call Stack (TID 25088):
    ucrtbased.dll!malloc()
    f:\dd\vctools\crt\vcstartup\src\heap\new_array.cpp (15): VLD_Test.exe!operator new[]() + 0x9 bytes
    c:\users\administrator\documents\vld_test\widget.cpp (9): VLD_Test.exe!Widget::Widget() + 0x7 bytes
    c:\users\administrator\documents\vld_test\main.cpp (9): VLD_Test.exe!main() + 0xA bytes
    c:\users\qt\work\qt\qtbase\src\winmain\qtmain_win.cpp (104): VLD_Test.exe!WinMain() + 0xD bytes
    f:\dd\vctools\crt\vcstartup\src\startup\exe_common.inl (109): VLD_Test.exe!invoke_main()
    f:\dd\vctools\crt\vcstartup\src\startup\exe_common.inl (264): VLD_Test.exe!__scrt_common_main_seh() + 0x5 bytes
    f:\dd\vctools\crt\vcstartup\src\startup\exe_common.inl (309): VLD_Test.exe!__scrt_common_main()
    f:\dd\vctools\crt\vcstartup\src\startup\exe_winmain.cpp (17): VLD_Test.exe!WinMainCRTStartup()
    KERNEL32.DLL!BaseThreadInitThunk() + 0x19 bytes
    ntdll.dll!RtlGetAppContainerNamedObjectPath() + 0xED bytes
    ntdll.dll!RtlGetAppContainerNamedObjectPath() + 0xBD bytes
  Data:
    CD CD CD CD    CD CD CD CD                                   ........ ........


Visual Leak Detector detected 2 memory leaks (96 bytes).
Largest number used: 318 bytes.
Total allocations: 318 bytes.
Visual Leak Detector is now exiting.

最后提示:Visual Leak Detector detected 2 memory leaks (96 bytes). 有两处内存泄露,并且提示信息中Data中也显示了内存泄露里的内容,并且提示到具体文件和行号:

  • 0
    点赞
  • 11
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值