当前位置: 首页 > news >正文

【cpp/c++ summary 工具】 vld(Visual Leak Detector)windows 内存泄漏检测工具

  • Visual Leak Detector,这是一个用于检测C/C++程序内存泄漏的工具。它可以在开发Windows应用程序时发现并修复内存泄漏的问题。

安装VLD

  • https://kinddragon.github.io/vld/
  • https://github.com/KindDragon/vld
    在这里插入图片描述

运行程序

在项目中包含头文件
  • 项目中,通常需要在main.cpp或相应的入口点文件中包含vld.h头文件。这样可以在程序启动时初始化VLD。
// main.cpp 或者项目的入口文件
#include "vld.h"
链接VLD库
  • 如果你使用的是Visual Studio,可以在项目属性中进行如下操作:
  1. 右键打开项目属性页面 (PropertiesAlt + Enter)。
  2. 转到VC++目录 -> 包含目录 添加VLD库的位置。
  3. 转到VC++目录 -> 库目录添加VLD.lib所在目录位置。

在这里插入图片描述

#include "vld.h"  // 引入VLD头文件
#include <iostream>int main() {std::cout << "Starting program..." << std::endl;int* p = new int(5);  // 分配内存// delete p;             // 释放内存std::cout << "Program finished." << std::endl;return 0;
}
  • 构建并运行程序。VLD会在程序退出时报告内存泄漏情况。
“testvld.exe”: 已加载“C:\Users\audit\Documents\Visual Studio 2010\Projects\testvld\Debug\testvld.exe”,已加载符号。
“testvld.exe”: 已加载“C:\Windows\SysWOW64\ntdll.dll”,Cannot find or open the PDB file
“testvld.exe”: 已加载“C:\Windows\SysWOW64\kernel32.dll”,Cannot find or open the PDB file
“testvld.exe”: 已加载“C:\Windows\SysWOW64\KernelBase.dll”,Cannot find or open the PDB file
“testvld.exe”: 已加载“C:\Windows\SysWOW64\msvcp100d.dll”,已加载符号。
“testvld.exe”: 已加载“C:\Program Files (x86)\Visual Leak Detector\bin\Win32\vld_x86.dll”,已加载符号。
“testvld.exe”: 已加载“C:\Windows\SysWOW64\msvcr100d.dll”,已加载符号。
“testvld.exe”: 已加载“C:\Windows\SysWOW64\advapi32.dll”,Cannot find or open the PDB file
“testvld.exe”: 已加载“C:\Windows\SysWOW64\msvcrt.dll”,Cannot find or open the PDB file
“testvld.exe”: 已加载“C:\Windows\SysWOW64\sechost.dll”,Cannot find or open the PDB file
“testvld.exe”: 已加载“C:\Windows\SysWOW64\bcrypt.dll”,Cannot find or open the PDB file
“testvld.exe”: 已加载“C:\Windows\SysWOW64\rpcrt4.dll”,Cannot find or open the PDB file
“testvld.exe”: 已加载“C:\Program Files (x86)\Visual Leak Detector\bin\Win32\dbghelp.dll”,Cannot find or open the PDB file
Visual Leak Detector read settings from: C:\Program Files (x86)\Visual Leak Detector\vld.ini
Visual Leak Detector Version 2.5.1 installed.
线程 'Win32 线程' (0x3328) 已退出,返回值为 0 (0x0)。
线程 'Win32 线程' (0x2144) 已退出,返回值为 0 (0x0)。
线程 'Win32 线程' (0x29dc) 已退出,返回值为 0 (0x0)。
WARNING: Visual Leak Detector detected memory leaks!
---------- Block 1 at 0x014248D8: 4 bytes ----------Leak Hash: 0xFDB1D2BB, Count: 1, Total 4 bytesCall Stack (TID 4060):MSVCR100D.dll!operator new()c:\users\audit\documents\visual studio 2010\projects\testvld\testvld\main1.cpp (7): testvld.exe!main() + 0x7 bytesf:\dd\vctools\crt_bld\self_x86\crt\src\crtexe.c (555): testvld.exe!__tmainCRTStartup() + 0x19 bytesf:\dd\vctools\crt_bld\self_x86\crt\src\crtexe.c (371): testvld.exe!mainCRTStartup()KERNEL32.DLL!BaseThreadInitThunk() + 0x19 bytesntdll.dll!RtlInitializeExceptionChain() + 0x6B bytesntdll.dll!RtlClearBits() + 0xBF bytesData:05 00 00 00                                                  ........ ........Visual Leak Detector detected 1 memory leak (40 bytes).
Largest number used: 40 bytes.
Total allocations: 40 bytes.
Visual Leak Detector is now exiting.
程序“[8448] testvld.exe: 本机”已退出,返回值为 0 (0x0)。

解决内存泄漏

  • 根据VLD提供的报告,检查代码中的内存分配和释放逻辑,以确保所有的newmalloc都有对应的deletefree调用。
#include "vld.h"  // 引入VLD头文件
#include <iostream>int main() {std::cout << "Starting program..." << std::endl;int* p = new int(5);  // 分配内存// delete p;             // 释放内存std::cout << "Program finished." << std::endl;return 0;
}
  • 输出:No memory leaks detected.
“testvld.exe”: 已加载“C:\Users\audit\Documents\Visual Studio 2010\Projects\testvld\Debug\testvld.exe”,已加载符号。
“testvld.exe”: 已加载“C:\Windows\SysWOW64\ntdll.dll”,Cannot find or open the PDB file
“testvld.exe”: 已加载“C:\Windows\SysWOW64\kernel32.dll”,Cannot find or open the PDB file
“testvld.exe”: 已加载“C:\Windows\SysWOW64\KernelBase.dll”,Cannot find or open the PDB file
“testvld.exe”: 已加载“C:\Windows\SysWOW64\msvcr100d.dll”,已加载符号。
“testvld.exe”: 已加载“C:\Windows\SysWOW64\msvcp100d.dll”,已加载符号。
“testvld.exe”: 已加载“C:\Program Files (x86)\Visual Leak Detector\bin\Win32\vld_x86.dll”,已加载符号。
“testvld.exe”: 已加载“C:\Windows\SysWOW64\advapi32.dll”,Cannot find or open the PDB file
“testvld.exe”: 已加载“C:\Windows\SysWOW64\msvcrt.dll”,Cannot find or open the PDB file
“testvld.exe”: 已加载“C:\Windows\SysWOW64\sechost.dll”,Cannot find or open the PDB file
“testvld.exe”: 已加载“C:\Windows\SysWOW64\bcrypt.dll”,Cannot find or open the PDB file
“testvld.exe”: 已加载“C:\Windows\SysWOW64\rpcrt4.dll”,Cannot find or open the PDB file
“testvld.exe”: 已加载“C:\Program Files (x86)\Visual Leak Detector\bin\Win32\dbghelp.dll”,Cannot find or open the PDB file
Visual Leak Detector read settings from: C:\Program Files (x86)\Visual Leak Detector\vld.ini
Visual Leak Detector Version 2.5.1 installed.
线程 'Win32 线程' (0xcd0) 已退出,返回值为 0 (0x0)。
线程 'Win32 线程' (0x3ca0) 已退出,返回值为 0 (0x0)。
线程 'Win32 线程' (0x3738) 已退出,返回值为 0 (0x0)。
No memory leaks detected.
Visual Leak Detector is now exiting.
程序“[10956] testvld.exe: 本机”已退出,返回值为 0 (0x0)。

http://www.mrgr.cn/news/41302.html

相关文章:

  • MDM监管锁系统ABM证书与MDM证书申请与使用
  • 【Python报错已解决】TypeError: ‘str‘ object cannot be interpreted as an integer
  • Shell文本处理(三)
  • 【橙子老哥】c# 探究属性与字段本质
  • 3271.哈希分割子串
  • Swift并发笔记
  • 【MySQL 06】表的增删查改
  • Mybatis-Flex使用
  • IP 协议的相关特性
  • 【信息系统项目管理师考题预测】范围管理
  • 土地规划与区域经济发展:筑基均衡未来的战略经纬
  • 利士策分享,行走•悟世•惜福: 旅行真谛
  • 告别 backtrader!换这个库实施量化回测
  • sed引入变量中的坑
  • marker=‘o‘, linestyle=‘-‘, color=‘b‘ plt.plot画图参数含义
  • ECharts图表图例4
  • image离散小波变换及pytorch_wavelets实现
  • A股暴涨,不更新文章了
  • 浙江大学《2022年+2023年845自动控制原理真题》 (完整版)
  • Kubernetes环境搭建