Windows平台上Visual Studio C/C++内存泄漏自动检查的工具VLD
VLD是一个用于Windows平台上Visual Studio C/C++内存泄漏自动检查的工具。
VLD通过对内存分配过程进行动态跟踪,在每次内存分配的时候记录其上下文,当程序退出时对检测到的内存泄漏查找其上下文信息,并转换成报告输出到Output中
VLD只能用于Windows平台下,不能用于Linux。
Linux平台下的内存泄漏检测,可以使用Valgrind
1. 安装VLD
安装前请关闭Visual Stuido
可到VLD的官网下载:http://vld.codeplex.com/
安装过程中全部接受默认选项与设置即可
2. 打开Visual Studio,检查一下VLD是否已经正确关联:
工具->选项->项目和解决方案->VC++目录->包含文件:确认VLD目录已在,默认安装路径是C:\Promgram Files(x86)\Visual Memory Detector\include
工具->选项->项目和解决方案->VC++目录->库文件:确认VLD目录已在,默认安装路径是C:\Promgram Files(x86)\Visual Memory Detector\lib\win32
3. 使用时,在包含入口函数的.cpp文件中包含vld.h就可以。
如果这个cpp文件中包含了预编译头文件stdafx.h,则将包含vld.h的语句放在stdafx.h的包含语句之后,否则放在最前面。
4. 创建一个试验项目:
#include<vld.h> //在文件的最前面包含VLD的头文件
#include<stdlib.h>
#include<stdio.h>
void f()
{
int *p = new int(0x12345678); //这里new了一个int,4个字节
printf("p=%08x, ", p);
}
int main()
{
f();
return 0;
}
5. 编译debug模式并执行(注意不要编译成发布release版本),在Visual Studio输出窗口中可以看到VLD的输出:
Visual Leak Detector read settings from: C:\Program Files (x86)\Visual Leak Detector\vld.ini
Visual Leak Detector Version 2.5 installed.
WARNING: Visual Leak Detector detected memory leaks!
---------- Block 1 at 0x001713E0: 4 bytes ---------- //VLD侦测到4个字节的泄漏
Leak Hash: 0xED71BAFA, Count: 1, Total 4 bytes
Call Stack (TID 3912):
Data:
78 56 34 12 xV4..... ........ //这是4个字节泄漏的内容:反序,所以也是12345678,也就是上面源码中设置的0x12345678
Visual Leak Detector detected 1 memory leak (40 bytes).
Largest number used: 40 bytes.
Total allocations: 40 bytes.
Visual Leak Detector is now exiting.
6. 在源码f()中加入对应的delete p;然后重新编译执行,这次输出显示无内存泄漏了:
Visual Leak Detector Version 2.5 installed.
No memory leaks detected.
Visual Leak Detector is now exiting.
VLD通过对内存分配过程进行动态跟踪,在每次内存分配的时候记录其上下文,当程序退出时对检测到的内存泄漏查找其上下文信息,并转换成报告输出到Output中
VLD只能用于Windows平台下,不能用于Linux。
Linux平台下的内存泄漏检测,可以使用Valgrind
1. 安装VLD
安装前请关闭Visual Stuido
可到VLD的官网下载:http://vld.codeplex.com/
安装过程中全部接受默认选项与设置即可
2. 打开Visual Studio,检查一下VLD是否已经正确关联:
工具->选项->项目和解决方案->VC++目录->包含文件:确认VLD目录已在,默认安装路径是C:\Promgram Files(x86)\Visual Memory Detector\include
工具->选项->项目和解决方案->VC++目录->库文件:确认VLD目录已在,默认安装路径是C:\Promgram Files(x86)\Visual Memory Detector\lib\win32
3. 使用时,在包含入口函数的.cpp文件中包含vld.h就可以。
如果这个cpp文件中包含了预编译头文件stdafx.h,则将包含vld.h的语句放在stdafx.h的包含语句之后,否则放在最前面。
4. 创建一个试验项目:
#include<vld.h> //在文件的最前面包含VLD的头文件
#include<stdlib.h>
#include<stdio.h>
void f()
{
int *p = new int(0x12345678); //这里new了一个int,4个字节
printf("p=%08x, ", p);
}
int main()
{
f();
return 0;
}
5. 编译debug模式并执行(注意不要编译成发布release版本),在Visual Studio输出窗口中可以看到VLD的输出:
Visual Leak Detector read settings from: C:\Program Files (x86)\Visual Leak Detector\vld.ini
Visual Leak Detector Version 2.5 installed.
WARNING: Visual Leak Detector detected memory leaks!
---------- Block 1 at 0x001713E0: 4 bytes ---------- //VLD侦测到4个字节的泄漏
Leak Hash: 0xED71BAFA, Count: 1, Total 4 bytes
Call Stack (TID 3912):
Data:
78 56 34 12 xV4..... ........ //这是4个字节泄漏的内容:反序,所以也是12345678,也就是上面源码中设置的0x12345678
Visual Leak Detector detected 1 memory leak (40 bytes).
Largest number used: 40 bytes.
Total allocations: 40 bytes.
Visual Leak Detector is now exiting.
6. 在源码f()中加入对应的delete p;然后重新编译执行,这次输出显示无内存泄漏了:
Visual Leak Detector Version 2.5 installed.
No memory leaks detected.
Visual Leak Detector is now exiting.
评论
发表评论