这类的工具有 比如 :LeakDiag leakfinder "Visual Leak Detector"
vld可以从http://vld.codeplex.com/releases/view/82311 现在最新版本,包括src源代码。
安装好以后,他会提示 要求添加 dll 到环境变量中去。
使用 vld 的方法为:在自己的代码中 添加 vld 的头文件,以及 lib 声明,其会自动去环境变量path中搜索 vld_x86.dll 或vld_x64.dll ,然后 调用其中的方法的。
头文件有俩:vld_def.h 和 vld.h,只需要包含后者(其会包含前者的)
贴下他们的源码、
1 vld_def.h代码 2 //// 3 // 4 // Visual Leak Detector - Import Library Header 5 // Copyright (c) 2005-2012 VLD Team 6 // 7 // This library is free software; you can redistribute it and/or 8 // modify it under the terms of the GNU Lesser General Public 9 // License as published by the Free Software Foundation; either 10 // version 2.1 of the License, or (at your option) any later version. 11 // 12 // This library is distributed in the hope that it will be useful, 13 // but WITHOUT ANY WARRANTY; without even the implied warranty of 14 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 15 // Lesser General Public License for more details. 16 // 17 // You should have received a copy of the GNU Lesser General Public 18 // License along with this library; if not, write to the Free Software 19 // Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA 20 // 21 // See COPYING.txt for the full terms of the GNU Lesser General Public License. 22 // 23 //// 24 25 #pragma once 26 27 #define VLD_OPT_AGGREGATE_DUPLICATES 0x0001 // If set, aggregate duplicate leaks in the leak report. 28 #define VLD_OPT_MODULE_LIST_INCLUDE 0x0002 // If set, modules in the module list are included, all others are excluded. 29 #define VLD_OPT_REPORT_TO_DEBUGGER 0x0004 // If set, the memory leak report is sent to the debugger. 30 #define VLD_OPT_REPORT_TO_FILE 0x0008 // If set, the memory leak report is sent to a file. 31 #define VLD_OPT_SAFE_STACK_WALK 0x0010 // If set, the stack is walked using the "safe" method (StackWalk64). 32 #define VLD_OPT_SELF_TEST 0x0020 // If set, perform a self-test to verify memory leak self-checking. 33 #define VLD_OPT_SLOW_DEBUGGER_DUMP 0x0040 // If set, inserts a slight delay between sending output to the debugger. 34 #define VLD_OPT_START_DISABLED 0x0080 // If set, memory leak detection will initially disabled. 35 #define VLD_OPT_TRACE_INTERNAL_FRAMES 0x0100 // If set, include useless frames (e.g. internal to VLD) in call stacks. 36 #define VLD_OPT_UNICODE_REPORT 0x0200 // If set, the leak report will be encoded UTF-16 instead of ASCII. 37 #define VLD_OPT_VLDOFF 0x0400 // If set, VLD will be completely deactivated. It will not attach to any modules. 38 #define VLD_OPT_REPORT_TO_STDOUT 0x0800 // If set, the memory leak report is sent to stdout. 39 #define VLD_OPT_SKIP_HEAPFREE_LEAKS 0x1000 // If set, VLD skip HeapFree memory leaks. 40 #define VLD_OPT_VALIDATE_HEAPFREE 0x2000 // If set, VLD verifies and reports heap consistency for HeapFree calls. 41 #define VLD_OPT_RELEASE_CRT_RUNTIME 0x4000 // If set, VLD treat CRT runtime as release version (use only with define VLD_FORCE_ENABLE). 42 43 #define VLD_RPTHOOK_INSTALL 0 44 #define VLD_RPTHOOK_REMOVE 1 45 46 typedef int (__cdecl * VLD_REPORT_HOOK)(int reportType, wchar_t *message, int *returnValue);
1 vld.h 2 3 //// 4 // 5 // Visual Leak Detector - Import Library Header 6 // Copyright (c) 2005-2012 VLD Team 7 // 8 // This library is free software; you can redistribute it and/or 9 // modify it under the terms of the GNU Lesser General Public 10 // License as published by the Free Software Foundation; either 11 // version 2.1 of the License, or (at your option) any later version. 12 // 13 // This library is distributed in the hope that it will be useful, 14 // but WITHOUT ANY WARRANTY; without even the implied warranty of 15 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 16 // Lesser General Public License for more details. 17 // 18 // You should have received a copy of the GNU Lesser General Public 19 // License along with this library; if not, write to the Free Software 20 // Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA 21 // 22 // See COPYING.txt for the full terms of the GNU Lesser General Public License. 23 // 24 //// 25 26 #pragma once 27 28 #include "vld_def.h" 29 30 #if defined _DEBUG || defined VLD_FORCE_ENABLE 31 32 #include <windows.h> 33 34 //#pragma comment(lib, "vld.lib") 35 36 // Force a symbolic reference to the global VisualLeakDetector class object from 37 // the DLL. This ensures that the DLL is loaded and linked with the program, 38 // even if no code otherwise imports any of the DLL's exports. 39 #pragma comment(linker, "/include:__imp_?g_vld@@3VVisualLeakDetector@@A") 40 41 //// 42 // 43 // Visual Leak Detector APIs 44 // 45 46 #ifdef __cplusplus 47 extern "C" { 48 #endif // __cplusplus 49 50 // VLDDisable - Disables Visual Leak Detector's memory leak detection at 51 // runtime. If memory leak detection is already disabled, then calling this 52 // function has no effect. 53 // 54 // Note: In multithreaded programs, this function operates on a per-thread 55 // basis. In other words, if you call this function from one thread, then 56 // memory leak detection is only disabled for that thread. If memory leak 57 // detection is enabled for other threads, then it will remain enabled for 58 // those other threads. It was designed to work this way to insulate you, 59 // the programmer, from having to ensure thread synchronization when calling 60 // VLDEnable() and VLDDisable(). Without this, calling these two functions 61 // unsynchronized could result in unpredictable and unintended behavior. 62 // But this also means that if you want to disable memory leak detection 63 // process-wide, then you need to call this function from every thread in 64 // the process. 65 // 66 // Return Value: 67 // 68 // None. 69 // 70 __declspec(dllimport) void VLDDisable (); 71 72 // VLDEnable - Enables Visual Leak Detector's memory leak detection at runtime. 73 // If memory leak detection is already enabled, which it is by default, then 74 // calling this function has no effect. 75 // 76 // Note: In multithreaded programs, this function operates on a per-thread 77 // basis. In other words, if you call this function from one thread, then 78 // memory leak detection is only enabled for that thread. If memory leak 79 // detection is disabled for other threads, then it will remain disabled for 80 // those other threads. It was designed to work this way to insulate you, 81 // the programmer, from having to ensure thread synchronization when calling 82 // VLDEnable() and VLDDisable(). Without this, calling these two functions 83 // unsynchronized could result in unpredictable and unintended behavior. 84 // But this also means that if you want to enable memory leak detection 85 // process-wide, then you need to call this function from every thread in 86 // the process. 87 // 88 // Return Value: 89 // 90 // None. 91 // 92 __declspec(dllimport) void VLDEnable (); 93 94 // VLDRestore - Restore Visual Leak Detector's previous state. 95 // 96 // Return Value: 97 // 98 // None. 99 // 100 __declspec(dllimport) void VLDRestore (); 101 102 // VLDGlobalDisable - Disables Visual Leak Detector's memory leak detection at 103 // runtime in all threads. If memory leak detection is already disabled, 104 // then calling this function has no effect. 105 // 106 // Return Value: 107 // 108 // None. 109 // 110 __declspec(dllimport) void VLDGlobalDisable (); 111 112 // VLDGlobalEnable - Enables Visual Leak Detector's memory leak detection 113 // at runtime in all threads. If memory leak detection is already enabled, 114 // which it is by default, then calling this function has no effect. 115 // 116 // Return Value: 117 // 118 // None. 119 // 120 __declspec(dllimport) void VLDGlobalEnable (); 121 122 // VLDReportLeaks - Report leaks up to the execution point. 123 // 124 // Return Value: 125 // 126 // None. 127 // 128 __declspec(dllimport) UINT VLDReportLeaks (); 129 130 // VLDGetLeaksCount - Return memory leaks count to the execution point. 131 // 132 // Return Value: 133 // 134 // None. 135 // 136 __declspec(dllimport) UINT VLDGetLeaksCount (); 137 138 // VLDMarkAllLeaksAsReported - Mark all leaks as reported. 139 // 140 // Return Value: 141 // 142 // None. 143 // 144 __declspec(dllimport) void VLDMarkAllLeaksAsReported (); 145 146 147 // VLDRefreshModules - Look for recently loaded DLLs and patch them if necessary. 148 // 149 // Return Value: 150 // 151 // None. 152 // 153 __declspec(dllimport) void VLDRefreshModules(); 154 155 156 // VLDEnableModule - Enable Memory leak checking on the specified module. 157 // 158 // module: module handle. 159 // 160 // Return Value: 161 // 162 // None. 163 // 164 165 __declspec(dllimport) void VLDEnableModule(HMODULE module); 166 167 168 // VLDDisableModule - Disable Memory leak checking on the specified module. 169 // 170 // module: module handle. 171 // 172 // Return Value: 173 // 174 // None. 175 // 176 __declspec(dllimport) void VLDDisableModule(HMODULE module); 177 178 // VLDGetOptions - Return all current options. 179 // 180 // Return Value: 181 // 182 // Mask of current options. 183 // 184 __declspec(dllimport) UINT VLDGetOptions(); 185 186 // VLDGetReportFilename - Return current report filename. 187 // 188 // filename: current report filename (max characters - MAX_PATH). 189 // 190 // Return Value: 191 // 192 // None. 193 // 194 __declspec(dllimport) void VLDGetReportFilename(WCHAR *filename); 195 196 // VLDSetOptions - Update the report options via function call rather than INI file. 197 // 198 // option_mask: Only the following flags are checked 199 // VLD_OPT_AGGREGATE_DUPLICATES 200 // VLD_OPT_MODULE_LIST_INCLUDE 201 // VLD_OPT_SAFE_STACK_WALK 202 // VLD_OPT_SLOW_DEBUGGER_DUMP 203 // VLD_OPT_TRACE_INTERNAL_FRAMES 204 // VLD_OPT_START_DISABLED 205 // VLD_OPT_SKIP_HEAPFREE_LEAKS 206 // VLD_OPT_VALIDATE_HEAPFREE 207 // 208 // maxDataDump: maximum number of user-data bytes to dump for each leaked block. 209 // 210 // maxTraceFrames: maximum number of frames per stack trace for each leaked block. 211 // 212 // Return Value: 213 // 214 // None. 215 // 216 __declspec(dllimport) void VLDSetOptions(UINT option_mask, SIZE_T maxDataDump, UINT maxTraceFrames); 217 218 // VLDSetModulesList - Set list of modules included/excluded in leak detection 219 // depending on parameter "includeModules". 220 // 221 // modules: list of modules to be forcefully included/excluded in leak detection. 222 // 223 // includeModules: include or exclude that modules. 224 // 225 // Return Value: 226 // 227 // None. 228 // 229 __declspec(dllimport) void VLDSetModulesList(CONST WCHAR *modules, BOOL includeModules); 230 231 // VLDGetModulesList - Return current list of included/excluded modules 232 // depending on flag VLD_OPT_TRACE_INTERNAL_FRAMES. 233 // 234 // modules: destination string for list of included/excluded modules (maximum length 512 characters). 235 // 236 // size: maximum string size. 237 // 238 // Return Value: 239 // 240 // BOOL: TRUE if include modules, otherwise FALSE. 241 // 242 __declspec(dllimport) BOOL VLDGetModulesList(WCHAR *modules, UINT size); 243 244 // VLDSetReportOptions - Update the report options via function call rather than INI file. 245 // 246 // Only the following flags are checked 247 // VLD_OPT_REPORT_TO_DEBUGGER 248 // VLD_OPT_REPORT_TO_FILE 249 // VLD_OPT_REPORT_TO_STDOUT 250 // VLD_OPT_UNICODE_REPORT 251 // 252 // filename is optional and can be NULL. 253 // 254 // Return Value: 255 // 256 // None. 257 // 258 __declspec(dllimport) void VLDSetReportOptions(UINT option_mask, CONST WCHAR *filename); 259 260 // VLDSetReportHook - Installs or uninstalls a client-defined reporting function by hooking it 261 // into the C run-time debug reporting process (debug version only). 262 // 263 // mode: The action to take: VLD_RPTHOOK_INSTALL or VLD_RPTHOOK_REMOVE. 264 // 265 // pfnNewHook: Report hook to install or remove. 266 // 267 // Return Value: 268 // 269 // int: 0 if success. 270 // 271 __declspec(dllimport) int VLDSetReportHook(int mode, VLD_REPORT_HOOK pfnNewHook); 272 273 // VLDResolveCallstacks - Performs symbol resolution for all saved extent CallStack's that have 274 // been tracked by Visual Leak Detector. This function is necessary for applications that 275 // dynamically load and unload modules, and through which memory leaks might be included. 276 // If this is NOT called, stack traces may have stack frames with no symbol information. This 277 // happens because the symbol API's cannot look up symbols for a binary / module that has been unloaded 278 // from the process. 279 // 280 // Return Value: 281 // 282 // None. 283 // 284 __declspec(dllexport) void VLDResolveCallstacks(); 285 286 #ifdef __cplusplus 287 } 288 #endif // __cplusplus 289 290 #else // !_DEBUG 291 292 #define VLDEnable() 293 #define VLDDisable() 294 #define VLDRestore() 295 #define VLDReportLeaks() 0 296 #define VLDGetLeaksCount() 0 297 #define VLDMarkAllLeaksAsReported() 298 #define VLDRefreshModules() 299 #define VLDEnableModule(a) 300 #define VLDDisableModule(b) 301 #define VLDGetOptions() 0 302 #define VLDGetReportFilename(a) 303 #define VLDSetOptions(a, b, c) 304 #define VLDSetReportHook(a, b) 305 #define VLDSetModulesList(a) 306 #define VLDGetModulesList(a, b) FALSE 307 #define VLDSetReportOptions(a, b) 308 309 #endif // _DEBUG
于是 我自己写了一个工程vldTest(用 vs2010 建立 console的 普通 的win32 程序)
下面就是测试的代码,lib和h文件 的路径 你自己看着办就行。vld.h里面也有 这个 包括 pragma lib的,注释掉 或者 将 lib添加到 path 还是 Library_Path什么环境变量中去。
下面的代码 功能是 写一个 内存泄漏 的程序,说白了,就是分配内存,但是没有释放掉。虽然程序结束会释放掉,但是如果不结束 一直 不释放的,就是内存泄漏了。下面程序 有2个内存泄漏,但是 vld 检测是3个。对了 编写成 DEBUG模式,才会启用 vld的功能。原因 看 vld.h的条件编译。
1 // vldTest.cpp : 定义控制台应用程序的入口点。 2 // 3 4 #include "stdafx.h" 5 #include <stdio.h> 6 #include <string.h> 7 #include <stdlib.h> 8 #include "..\include\vld.h" 9 10 #pragma comment(lib,"../lib/Win32/vld.lib") 11 12 13 14 class MyTest 15 { 16 public: 17 MyTest(const char *szName) 18 { 19 // The following is the second resulting leak 20 m_pszName = strdup(szName); 21 } 22 ~MyTest() 23 { 24 if (m_pszName != NULL) 25 free(m_pszName); 26 m_pszName = NULL; 27 } 28 protected: 29 char *m_pszName; 30 }; 31 32 33 34 int _tmain(int argc, _TCHAR* argv[]) 35 { 36 int * ptrInt; 37 ptrInt=(int*)malloc(10); 38 memset(ptrInt,0xed,10); 39 printf("0x%08x\n",*ptrInt); 40 //VLDEnable(); 41 //VLDRestore(); 42 //VLDGlobalEnable(); 43 44 45 // This is the "main" leak 46 MyTest *pTest = new MyTest("This is an example"); 47 //VLDReportLeaks(); 48 //VLDGetLeaksCount (); 49 50 51 return 0; 52 }
运行效果如图:(为了显示全部,去掉了MyTest 那句话)。
如果 加上 free(ptrInt); 就没有泄漏了。如图
除了 0xedededed 这句话 其他都是 vld 的输出。如果发布成 release,默认 不会 调用 vld了。
程序参考了
http://topic.csdn.net/t/20021216/13/1265024.html
http://www.codeproject.com/Articles/3134/Memory-Leak-and-Exception-Trace-CRT-and-COM-Leaks
转载请注明出处:http://www.cnblogs.com/ayanmw 多谢
------------------------------------------------------------------------------------------------
一定要专业!本博客定位于ArcGIS开发,C语言,C++语言,Java语言,Android开发和少量的Web开发,之前是做Web开发的,其实就是ASP维护,发现EasyASP这个好框架,对前端后端数据库 都很感觉亲切啊。.
所有评论(0)