vld 检测dll中是否有c 内存泄漏检测工具

Visual LeakDetector(VLD)是一款用于Visual C++的免费的内存泄露检测工具。它的特点有:(1)、它是免费开源的,采用LGPL协议;(2)、它可以得到内存泄露点的调用堆栈,可以获取到所在文件及行号;(3)、它可以得到泄露内存的完整数据;(4)、它可以设置内存泄露报告的级别。
&&&&&&&& 默认情况下,只有在Debug模式下才会启用VLD的功能。
&&&&&&&& 在使用的时候只要将VLD的头文件和lib文件放在工程文件中即可。
&&&&&&&& 在需要检测内存泄露的源文件中添加#include “vld.h”
&&&&&&&& VLD只能在Windows下使用,它应用在C/C++语言中。
&&&&&&&& 通过修改vld.ini文件(ReportFile选项),可以设置内存泄露报告的保存位置。
&&&&&&&& VLD好像对中文路径不支持。
&&&&&&&& VLD的各个版本对应适用于不同的VC/VS编译器,注意选择合适的版本。
1.&&&&&&&&从下载最新源代码,解压缩,里面包含有源码、vs2010工程配置、以及一些依赖库,如dbghelp等;
2.&&&&&&&&&&打开vld_vs10.sln,在32位/64位下,分别编译debug和release下vld工程,会在当前目录下生成bin文件夹,里面包含相应的库文件;
3.&&&&&&&&&&新建控制台工程VLDtest,在Debug下运行,代码如下:
stdafx.cpp:
VLDtest.cpp:
4.&&&&&&&&&&运行提示,“无法启动此程序,因为计算机中丢失vld_x86.dll”,解决方法,将bin/Win32/Debug/vld_x86.dll拷贝到VLDtest执行目录下或将vld_x86.dll路径添加到系统环境变量中;
5.&&&&&&&&&&运行提示,“应用程序无法正常启动(0xc0150002)”,将源代码中的/setup/dbghelp/x86文件夹下的dbghelp.dll和Microsoft.DTfW.DHL.manifest两个文件拷贝到VLDtest执行目录下即可;
6.&&&&&&&&&&直接按F5运行,可以在Output窗口显示内存泄漏的相关信息,包括发生内存泄漏的文件名和行号,双击即可定位到内存泄漏的具体位置;
7.&&&&&&&&&&或者直接运行,相关信息可以显示在命令行窗口中;
8.&&&&&&&&也可将内存泄漏信息写入文件中,将源代码中的vld.ini文件拷贝到VLDtest工程目录下,即与VLDtest.vcxproj同目录,修改vld.ini文件中的选项参数,ReportFile = .\../../../lib/dbg/x86_vc10/memory_leak_report.txt,ReportTo =both(原始ReportTo = debugger),重新运行后,会在执行目录下生成一个memory_leak_report.txt文件;或者将vld.ini文件拷贝到执行目录下,运行VLDtest[dbg_x86_vc10].exe文件也可生成报告文件;
9.&&&&&&&&&&在Release下运行,VLD不起作用,VLDtest工程不用作任何配置和调整;
10.& 也可不用编译源代码,直接从下载,然后安装即可,里面包含需要的库和头文件。
图中为运行测试代码时显示的内存泄漏信息:
参考知识库
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
访问:8564次
排名:千里之外
原创:21篇
转载:23篇
(2)(1)(2)(14)(18)(7)用VLD检测内存泄漏总是有问题,烦躁啊_c++吧_百度贴吧
&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&签到排名:今日本吧第个签到,本吧因你更精彩,明天继续来努力!
本吧签到人数:0成为超级会员,使用一键签到本月漏签0次!成为超级会员,赠送8张补签卡连续签到:天&&累计签到:天超级会员单次开通12个月以上,赠送连续签到卡3张
关注:238,087贴子:
用VLD检测内存泄漏总是有问题,烦躁啊收藏
我用VLD检测编写的动态库,是不是VLD只能检测exe?
登录百度帐号推荐应用
为兴趣而生,贴吧更懂你。或C++基础(54)
你看到的这个文章来自于/ayanmw
这类的工具有 比如 :LeakDiag leakfinder &Visual Leak Detector&  
vld可以从&现在最新版本,包括src源代码。
安装好以后,他会提示 要求添加 dll 到环境变量中去。
使用 vld 的方法为:在自己的代码中 添加 vld 的头文件,以及 lib 声明,其会自动去环境变量path中搜索 vld_x86.dll 或vld_x64.dll ,然后 调用其中的方法的。
头文件有俩:vld_def.h 和 vld.h,只需要包含后者(其会包含前者的)
贴下他们的源码、
vld_def.h代码
这 vld 并没有提供sample,提供的src源代码 也只是 编译成 dll的。
于是 我自己写了一个工程vldTest(用 vs2010 建立 console的 普通 的win32 程序)
下面就是测试的代码,lib和h文件 的路径 你自己看着办就行。vld.h里面也有 这个 包括 pragma lib的,注释掉 或者& 将 lib添加到 path 还是 Library_Path什么环境变量中去。
下面的代码 功能是 写一个 内存泄漏 的程序,说白了,就是分配内存,但是没有释放掉。虽然程序结束会释放掉,但是如果不结束 一直 不释放的,就是内存泄漏了。下面程序 有2个内存泄漏,但是 vld 检测是3个。对了 编写成 DEBUG模式,才会启用 vld的功能。原因 看 vld.h的条件编译。
// vldTest.cpp : 定义控制台应用程序的入口点。
#include &stdafx.h&
#include &stdio.h&
#include &string.h&
#include &stdlib.h&
#include &..\include\vld.h&
#pragma comment(lib,&../lib/Win32/vld.lib&)
class MyTest
MyTest(const char *szName)
// The following is the second resulting leak
m_pszName = strdup(szName);
if (m_pszName != NULL)
free(m_pszName);
m_pszName = NULL;
protected:
char *m_pszN
int _tmain(int argc, _TCHAR* argv[])
int * ptrI
ptrInt=(int*)malloc(<span style="color:#);
memset(ptrInt,<span style="color:#xed,<span style="color:#);
printf(&<span style="color:#x%08x\n&,*ptrInt);
//VLDEnable();
//VLDRestore();
//VLDGlobalEnable();
// This is the &main& leak
MyTest *pTest = new MyTest(&This is an example&);
//VLDReportLeaks();
//VLDGetLeaksCount ();
return <span style="color:#;
运行效果如图:(为了显示全部,去掉了MyTest 那句话)。
如果 加上 free(ptrInt); 就没有泄漏了。如图
除了 0xedededed 这句话 其他都是 vld 的输出。如果发布成 release,默认 不会 调用 vld了。
程序参考了
转载请注明出处:/ayanmw 我会很高兴的!
参考知识库
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
访问:63423次
积分:1311
积分:1311
排名:千里之外
原创:44篇
转载:151篇
(1)(1)(1)(2)(22)(1)(2)(2)(2)(1)(2)(8)(7)(1)(6)(2)(4)(5)(4)(7)(21)(2)(6)(13)(20)(22)(23)(3)(2)(4)1655人阅读
这类的工具有 比如 :LeakDiag leakfinder &Visual Leak Detector&  
vld可以从&现在最新版本,包括src源代码。
安装好以后,他会提示 要求添加 dll 到环境变量中去。
使用 vld 的方法为:在自己的代码中 添加 vld 的头文件,以及 lib 声明,其会自动去环境变量path中搜索 vld_x86.dll 或vld_x64.dll ,然后 调用其中的方法的。
头文件有俩:vld_def.h 和 vld.h,只需要包含后者(其会包含前者的)
贴下他们的源码、
vld_def.h代码
1 ////////////////////////////////////////////////////////////////////////////////
Visual Leak Detector - Import Library Header
Copyright (c)
This libr you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software F either
version 2.1 of the License, or (at your option) any later version.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
See the GNU
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License alo if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
See COPYING.txt for the full terms of the GNU Lesser General Public License.
22 ////////////////////////////////////////////////////////////////////////////////
24 #pragma once
26 #define VLD_OPT_AGGREGATE_DUPLICATES
If set, aggregate duplicate leaks in the leak report.
27 #define VLD_OPT_MODULE_LIST_INCLUDE
If set, modules in the module list are included, all others are excluded.
28 #define VLD_OPT_REPORT_TO_DEBUGGER
If set, the memory leak report is sent to the debugger.
29 #define VLD_OPT_REPORT_TO_FILE
If set, the memory leak report is sent to a file.
30 #define VLD_OPT_SAFE_STACK_WALK
If set, the stack is walked using the &safe& method (StackWalk64).
31 #define VLD_OPT_SELF_TEST
If set, perform a self-test to verify memory leak self-checking.
32 #define VLD_OPT_SLOW_DEBUGGER_DUMP
If set, inserts a slight delay between sending output to the debugger.
33 #define VLD_OPT_START_DISABLED
If set, memory leak detection will initially disabled.
34 #define VLD_OPT_TRACE_INTERNAL_FRAMES
If set, include useless frames (e.g. internal to VLD) in call stacks.
35 #define VLD_OPT_UNICODE_REPORT
If set, the leak report will be encoded UTF-16 instead of ASCII.
36 #define VLD_OPT_VLDOFF
If set, VLD will be completely deactivated. It will not attach to any modules.
37 #define VLD_OPT_REPORT_TO_STDOUT
If set, the memory leak report is sent to stdout.
38 #define VLD_OPT_SKIP_HEAPFREE_LEAKS
If set, VLD skip HeapFree memory leaks.
39 #define VLD_OPT_VALIDATE_HEAPFREE
If set, VLD verifies and reports heap consistency for HeapFree calls.
40 #define VLD_OPT_RELEASE_CRT_RUNTIME
If set, VLD treat CRT runtime as release version (use only with define VLD_FORCE_ENABLE).
42 #define VLD_RPTHOOK_INSTALL
43 #define VLD_RPTHOOK_REMOVE
45 typedef int (__cdecl * VLD_REPORT_HOOK)(int reportType, wchar_t *message, int *returnValue);
////////////////////////////////////////////////////////////////////////////////
Visual Leak Detector - Import Library Header
Copyright (c)
This libr you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software F either
version 2.1 of the License, or (at your option) any later version.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
See the GNU
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License alo if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
See COPYING.txt for the full terms of the GNU Lesser General Public License.
////////////////////////////////////////////////////////////////////////////////
#pragma once
#include &vld_def.h&
#if defined _DEBUG || defined VLD_FORCE_ENABLE
#include &windows.h&
//#pragma comment(lib, &vld.lib&)
// Force a symbolic reference to the global VisualLeakDetector class object from
// the DLL. This ensures that the DLL is loaded and linked with the program,
// even if no code otherwise imports any of the DLL's exports.
#pragma comment(linker, &/include:__imp_?g_vld@@3VVisualLeakDetector@@A&)
////////////////////////////////////////////////////////////////////////////////
Visual Leak Detector APIs
#ifdef __cplusplus
extern &C& {
#endif // __cplusplus
// VLDDisable - Disables Visual Leak Detector's memory leak detection at
runtime. If memory leak detection is already disabled, then calling this
function has no effect.
Note: In multithreaded programs, this function operates on a per-thread
basis. In other words, if you call this function from one thread, then
memory leak detection is only disabled for that thread. If memory leak
detection is enabled for other threads, then it will remain enabled for
those other threads. It was designed to work this way to insulate you,
the programmer, from having to ensure thread synchronization when calling
VLDEnable() and VLDDisable(). Without this, calling these two functions
unsynchronized could result in unpredictable and unintended behavior.
But this also means that if you want to disable memory leak detection
process-wide, then you need to call this function from every thread in
the process.
Return Value:
__declspec(dllimport) void VLDDisable ();
// VLDEnable - Enables Visual Leak Detector's memory leak detection at runtime.
If memory leak detection is already enabled, which it is by default, then
calling this function has no effect.
Note: In multithreaded programs, this function operates on a per-thread
basis. In other words, if you call this function from one thread, then
memory leak detection is only enabled for that thread. If memory leak
detection is disabled for other threads, then it will remain disabled for
those other threads. It was designed to work this way to insulate you,
the programmer, from having to ensure thread synchronization when calling
VLDEnable() and VLDDisable(). Without this, calling these two functions
unsynchronized could result in unpredictable and unintended behavior.
But this also means that if you want to enable memory leak detection
process-wide, then you need to call this function from every thread in
the process.
Return Value:
__declspec(dllimport) void VLDEnable ();
// VLDRestore - Restore Visual Leak Detector's previous state.
Return Value:
__declspec(dllimport) void VLDRestore ();
// VLDGlobalDisable - Disables Visual Leak Detector's memory leak detection at
runtime in all threads. If memory leak detection is already disabled,
then calling this function has no effect.
Return Value:
__declspec(dllimport) void VLDGlobalDisable ();
// VLDGlobalEnable - Enables Visual Leak Detector's memory leak detection
at runtime in all threads. If memory leak detection is already enabled,
which it is by default, then calling this function has no effect.
Return Value:
__declspec(dllimport) void VLDGlobalEnable ();
// VLDReportLeaks - Report leaks up to the execution point.
Return Value:
__declspec(dllimport) UINT VLDReportLeaks ();
// VLDGetLeaksCount - Return memory leaks count to the execution point.
Return Value:
__declspec(dllimport) UINT VLDGetLeaksCount ();
// VLDMarkAllLeaksAsReported - Mark all leaks as reported.
Return Value:
__declspec(dllimport) void VLDMarkAllLeaksAsReported ();
// VLDRefreshModules - Look for recently loaded DLLs and patch them if necessary.
Return Value:
__declspec(dllimport) void VLDRefreshModules();
// VLDEnableModule - Enable Memory leak checking on the specified module.
// module: module handle.
Return Value:
__declspec(dllimport) void VLDEnableModule(HMODULE module);
// VLDDisableModule - Disable Memory leak checking on the specified module.
// module: module handle.
Return Value:
__declspec(dllimport) void VLDDisableModule(HMODULE module);
// VLDGetOptions - Return all current options.
Return Value:
Mask of current options.
__declspec(dllimport) UINT VLDGetOptions();
// VLDGetReportFilename - Return current report filename.
// filename: current report filename (max characters - MAX_PATH).
Return Value:
__declspec(dllimport) void VLDGetReportFilename(WCHAR *filename);
// VLDSetOptions - Update the report options via function call rather than INI file.
// option_mask: Only the following flags are checked
// VLD_OPT_AGGREGATE_DUPLICATES
// VLD_OPT_MODULE_LIST_INCLUDE
// VLD_OPT_SAFE_STACK_WALK
// VLD_OPT_SLOW_DEBUGGER_DUMP
// VLD_OPT_TRACE_INTERNAL_FRAMES
// VLD_OPT_START_DISABLED
// VLD_OPT_SKIP_HEAPFREE_LEAKS
// VLD_OPT_VALIDATE_HEAPFREE
// maxDataDump: maximum number of user-data bytes to dump for each leaked block.
// maxTraceFrames: maximum number of frames per stack trace for each leaked block.
Return Value:
__declspec(dllimport) void VLDSetOptions(UINT option_mask, SIZE_T maxDataDump, UINT maxTraceFrames);
// VLDSetModulesList - Set list of modules included/excluded in leak detection
// depending on parameter &includeModules&.
// modules: list of modules to be forcefully included/excluded in leak detection.
// includeModules: include or exclude that modules.
Return Value:
__declspec(dllimport) void VLDSetModulesList(CONST WCHAR *modules, BOOL includeModules);
// VLDGetModulesList - Return current list of included/excluded modules
// depending on flag VLD_OPT_TRACE_INTERNAL_FRAMES.
// modules: destination string for list of included/excluded modules (maximum length 512 characters).
// size: maximum string size.
Return Value:
BOOL: TRUE if include modules, otherwise FALSE.
__declspec(dllimport) BOOL VLDGetModulesList(WCHAR *modules, UINT size);
// VLDSetReportOptions - Update the report options via function call rather than INI file.
// Only the following flags are checked
// VLD_OPT_REPORT_TO_DEBUGGER
// VLD_OPT_REPORT_TO_FILE
// VLD_OPT_REPORT_TO_STDOUT
// VLD_OPT_UNICODE_REPORT
// filename is optional and can be NULL.
Return Value:
__declspec(dllimport) void VLDSetReportOptions(UINT option_mask, CONST WCHAR *filename);
// VLDSetReportHook - Installs or uninstalls a client-defined reporting function by hooking it
into the C run-time debug reporting process (debug version only).
// mode: The action to take: VLD_RPTHOOK_INSTALL or VLD_RPTHOOK_REMOVE.
// pfnNewHook: Report hook to install or remove.
Return Value:
int: 0 if success.
__declspec(dllimport) int VLDSetReportHook(int mode,
VLD_REPORT_HOOK pfnNewHook);
// VLDResolveCallstacks - Performs symbol resolution for all saved extent CallStack's that have
// been tracked by Visual Leak Detector. This function is necessary for applications that
// dynamically load and unload modules, and through which memory leaks might be included.
// If this is NOT called, stack traces may have stack frames with no symbol information. This
// happens because the symbol API's cannot look up symbols for a binary / module that has been unloaded
// from the process.
Return Value:
__declspec(dllexport) void VLDResolveCallstacks();
#ifdef __cplusplus
#endif // __cplusplus
#else // !_DEBUG
#define VLDEnable()
#define VLDDisable()
#define VLDRestore()
#define VLDReportLeaks() 0
#define VLDGetLeaksCount() 0
#define VLDMarkAllLeaksAsReported()
#define VLDRefreshModules()
#define VLDEnableModule(a)
#define VLDDisableModule(b)
#define VLDGetOptions() 0
#define VLDGetReportFilename(a)
#define VLDSetOptions(a, b, c)
#define VLDSetReportHook(a, b)
#define VLDSetModulesList(a)
#define VLDGetModulesList(a, b) FALSE
#define VLDSetReportOptions(a, b)
#endif // _DEBUG
这 vld 并没有提供sample,提供的src源代码 也只是 编译成 dll的。
于是 我自己写了一个工程vldTest(用 vs2010 建立 console的 普通 的win32 程序)
下面就是测试的代码,lib和h文件 的路径 你自己看着办就行。vld.h里面也有 这个 包括 pragma lib的,注释掉 或者& 将 lib添加到 path 还是 Library_Path什么环境变量中去。
下面的代码 功能是 写一个 内存泄漏 的程序,说白了,就是分配内存,但是没有释放掉。虽然程序结束会释放掉,但是如果不结束 一直 不释放的,就是内存泄漏了。下面程序 有2个内存泄漏,但是 vld 检测是3个。对了 编写成 DEBUG模式,才会启用 vld的功能。原因 看 vld.h的条件编译。
// vldTest.cpp : 定义控制台应用程序的入口点。
#include &stdafx.h&
#include &stdio.h&
#include &string.h&
#include &stdlib.h&
#include &..\include\vld.h&
#pragma comment(lib,&../lib/Win32/vld.lib&)
class MyTest
MyTest(const char *szName)
// The following is the second resulting leak
m_pszName = strdup(szName);
if (m_pszName != NULL)
free(m_pszName);
m_pszName = NULL;
protected:
char *m_pszN
int _tmain(int argc, _TCHAR* argv[])
int * ptrI
ptrInt=(int*)malloc(10);
memset(ptrInt,0xed,10);
printf(&<span style="color:#x\n&,*ptrInt);
//VLDEnable();
//VLDRestore();
//VLDGlobalEnable();
// This is the &main& leak
MyTest *pTest = new MyTest(&This is an example&);
//VLDReportLeaks();
//VLDGetLeaksCount ();
运行效果如图:(为了显示全部,去掉了MyTest 那句话)。
如果 加上 free(ptrInt); 就没有泄漏了。如图
除了 0xedededed 这句话 其他都是 vld 的输出。如果发布成 release,默认 不会 调用 vld了。
程序参考了
参考知识库
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
访问:107582次
积分:1251
积分:1251
排名:千里之外
原创:11篇
评论:42条
(1)(2)(5)(5)(4)(1)(1)(6)(1)

我要回帖

更多关于 ios 内存泄漏检测工具 的文章

 

随机推荐