自学内容网 自学内容网

C++——内存检测工具Visual Leak Detector的安装以及使用详解

目录

一、Visual Leak Detector的安装

1、Visual Leak Detector安装包下载

2、安装Visual Leak Detector

二、配置环境变量

三、使用测试


一、Visual Leak Detector的安装

1、Visual Leak Detector安装包下载

Visual Leak Detector | Enhanced Memory Leak Detection for Visual C++ (kinddragon.github.io)icon-default.png?t=N7T8https://kinddragon.github.io/vld/

2、安装Visual Leak Detector

双击安装包,进行安装

点击下一步

二、配置环境变量

打开刚刚自己对应选择的安装路径文件夹下,有如下文件

打开开始菜单,系统选项卡,选择右边高级系统设置

选择环境变量

双击path后面,横线上的,进行添加环境变量

添加对应路径,根据刚刚配置的文件路径进行设置

三、使用测试

打开Vistual Studio2019,或其它版本,新建一个c++程序,添加库目录等

将如下测试代码加入进去

#include "vld.h"

#include <iostream>

using namespace std;

int main(void)
{
int x = 5;

int* y;
y = new int(16);

cout << *y;

int* w = new int(4);

cout << *w << endl;

cout << x << endl;
}

然后点击运行,这里我们能看到准确的泄漏行号,以及泄露的字节数、数据,如下

Visual Leak Detector read settings from: C:\Program Files (x86)\Visual Leak Detector\vld.ini
Visual Leak Detector Version 2.5.1 installed.
164
5
WARNING: Visual Leak Detector detected memory leaks!
---------- Block 2 at 0x00000000E2C5E330: 4 bytes ----------
  Leak Hash: 0xEFCA66D4, Count: 1, Total 4 bytes
  Call Stack (TID 15924):
    ucrtbased.dll!malloc()
    D:\a\_work\1\s\src\vctools\crt\vcstartup\src\heap\new_scalar.cpp (35): testVldDemo.exe!operator new() + 0xA bytes
    F:\C++\testVldDemo\main.cpp (16): testVldDemo.exe!main() + 0xA bytes
    D:\a\_work\1\s\src\vctools\crt\vcstartup\src\startup\exe_common.inl (79): testVldDemo.exe!invoke_main()
    D:\a\_work\1\s\src\vctools\crt\vcstartup\src\startup\exe_common.inl (288): testVldDemo.exe!__scrt_common_main_seh() + 0x5 bytes
    D:\a\_work\1\s\src\vctools\crt\vcstartup\src\startup\exe_common.inl (331): testVldDemo.exe!__scrt_common_main()
    D:\a\_work\1\s\src\vctools\crt\vcstartup\src\startup\exe_main.cpp (17): testVldDemo.exe!mainCRTStartup()
    KERNEL32.DLL!BaseThreadInitThunk() + 0x14 bytes
    ntdll.dll!RtlUserThreadStart() + 0x21 bytes
  Data:
    04 00 00 00                                                  ........ ........


---------- Block 1 at 0x00000000E2C5EDF0: 4 bytes ----------
  Leak Hash: 0x35E4CF76, Count: 1, Total 4 bytes
  Call Stack (TID 15924):
    ucrtbased.dll!malloc()
    D:\a\_work\1\s\src\vctools\crt\vcstartup\src\heap\new_scalar.cpp (35): testVldDemo.exe!operator new() + 0xA bytes
    F:\C++\testVldDemo\main.cpp (12): testVldDemo.exe!main() + 0xA bytes
    D:\a\_work\1\s\src\vctools\crt\vcstartup\src\startup\exe_common.inl (79): testVldDemo.exe!invoke_main()
    D:\a\_work\1\s\src\vctools\crt\vcstartup\src\startup\exe_common.inl (288): testVldDemo.exe!__scrt_common_main_seh() + 0x5 bytes
    D:\a\_work\1\s\src\vctools\crt\vcstartup\src\startup\exe_common.inl (331): testVldDemo.exe!__scrt_common_main()
    D:\a\_work\1\s\src\vctools\crt\vcstartup\src\startup\exe_main.cpp (17): testVldDemo.exe!mainCRTStartup()
    KERNEL32.DLL!BaseThreadInitThunk() + 0x14 bytes
    ntdll.dll!RtlUserThreadStart() + 0x21 bytes
  Data:
    10 00 00 00                 

原文地址:https://blog.csdn.net/m0_65635427/article/details/140370733

免责声明:本站文章内容转载自网络资源,如本站内容侵犯了原著者的合法权益,可联系本站删除。更多内容请关注自学内容网(zxcms.com)!