这类的工具有 比如 :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,只需要包含后者(其会包含前者的)
贴下他们的源码、
vld_def.h代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46 11 ////////////////////////////////////////////////////////////////////////////////
2 2 //
3 3 // Visual Leak Detector - Import Library Header
4 4 // Copyright (c) 2005-2012 VLD Team
5 5 //
6 6 // This library is free software; you can redistribute it and/or
7 7 // modify it under the terms of the GNU Lesser General Public
8 8 // License as published by the Free Software Foundation; either
9 9 // version 2.1 of the License, or (at your option) any later version.
1010 //
1111 // This library is distributed in the hope that it will be useful,
1212 // but WITHOUT ANY WARRANTY; without even the implied warranty of
1313 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
1414 // Lesser General Public License for more details.
1515 //
1616 // You should have received a copy of the GNU Lesser General Public
1717 // License along with this library; if not, write to the Free Software
1818 // Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
1919 //
2020 // See COPYING.txt for the full terms of the GNU Lesser General Public License.
2121 //
2222 ////////////////////////////////////////////////////////////////////////////////
2323
2424 #pragma once
2525
2626 #define VLD_OPT_AGGREGATE_DUPLICATES 0x0001 // If set, aggregate duplicate leaks in the leak report.
2727 #define VLD_OPT_MODULE_LIST_INCLUDE 0x0002 // If set, modules in the module list are included, all others are excluded.
2828 #define VLD_OPT_REPORT_TO_DEBUGGER 0x0004 // If set, the memory leak report is sent to the debugger.
2929 #define VLD_OPT_REPORT_TO_FILE 0x0008 // If set, the memory leak report is sent to a file.
3030 #define VLD_OPT_SAFE_STACK_WALK 0x0010 // If set, the stack is walked using the "safe" method (StackWalk64).
3131 #define VLD_OPT_SELF_TEST 0x0020 // If set, perform a self-test to verify memory leak self-checking.
3232 #define VLD_OPT_SLOW_DEBUGGER_DUMP 0x0040 // If set, inserts a slight delay between sending output to the debugger.
3333 #define VLD_OPT_START_DISABLED 0x0080 // If set, memory leak detection will initially disabled.
3434 #define VLD_OPT_TRACE_INTERNAL_FRAMES 0x0100 // If set, include useless frames (e.g. internal to VLD) in call stacks.
3535 #define VLD_OPT_UNICODE_REPORT 0x0200 // If set, the leak report will be encoded UTF-16 instead of ASCII.
3636 #define VLD_OPT_VLDOFF 0x0400 // If set, VLD will be completely deactivated. It will not attach to any modules.
3737 #define VLD_OPT_REPORT_TO_STDOUT 0x0800 // If set, the memory leak report is sent to stdout.
3838 #define VLD_OPT_SKIP_HEAPFREE_LEAKS 0x1000 // If set, VLD skip HeapFree memory leaks.
3939 #define VLD_OPT_VALIDATE_HEAPFREE 0x2000 // If set, VLD verifies and reports heap consistency for HeapFree calls.
4040 #define VLD_OPT_RELEASE_CRT_RUNTIME 0x4000 // If set, VLD treat CRT runtime as release version (use only with define VLD_FORCE_ENABLE).
4141
4242 #define VLD_RPTHOOK_INSTALL 0
4343 #define VLD_RPTHOOK_REMOVE 1
4444
4545 typedef int (__cdecl * VLD_REPORT_HOOK)(int reportType, wchar_t *message, int *returnValue);
46
vld.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308 1////////////////////////////////////////////////////////////////////////////////
2//
3// Visual Leak Detector - Import Library Header
4// Copyright (c) 2005-2012 VLD Team
5//
6// This library is free software; you can redistribute it and/or
7// modify it under the terms of the GNU Lesser General Public
8// License as published by the Free Software Foundation; either
9// version 2.1 of the License, or (at your option) any later version.
10//
11// This library is distributed in the hope that it will be useful,
12// but WITHOUT ANY WARRANTY; without even the implied warranty of
13// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14// Lesser General Public License for more details.
15//
16// You should have received a copy of the GNU Lesser General Public
17// License along with this library; if not, write to the Free Software
18// Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
19//
20// See COPYING.txt for the full terms of the GNU Lesser General Public License.
21//
22////////////////////////////////////////////////////////////////////////////////
23
24#pragma once
25
26#include "vld_def.h"
27
28#if defined _DEBUG || defined VLD_FORCE_ENABLE
29
30#include <windows.h>
31
32//#pragma comment(lib, "vld.lib")
33
34// Force a symbolic reference to the global VisualLeakDetector class object from
35// the DLL. This ensures that the DLL is loaded and linked with the program,
36// even if no code otherwise imports any of the DLL's exports.
37#pragma comment(linker, "/include:__imp_?g_vld@@3VVisualLeakDetector@@A")
38
39////////////////////////////////////////////////////////////////////////////////
40//
41// Visual Leak Detector APIs
42//
43
44#ifdef __cplusplus
45extern "C" {
46#endif // __cplusplus
47
48// VLDDisable - Disables Visual Leak Detector's memory leak detection at
49// runtime. If memory leak detection is already disabled, then calling this
50// function has no effect.
51//
52// Note: In multithreaded programs, this function operates on a per-thread
53// basis. In other words, if you call this function from one thread, then
54// memory leak detection is only disabled for that thread. If memory leak
55// detection is enabled for other threads, then it will remain enabled for
56// those other threads. It was designed to work this way to insulate you,
57// the programmer, from having to ensure thread synchronization when calling
58// VLDEnable() and VLDDisable(). Without this, calling these two functions
59// unsynchronized could result in unpredictable and unintended behavior.
60// But this also means that if you want to disable memory leak detection
61// process-wide, then you need to call this function from every thread in
62// the process.
63//
64// Return Value:
65//
66// None.
67//
68__declspec(dllimport) void VLDDisable ();
69
70// VLDEnable - Enables Visual Leak Detector's memory leak detection at runtime.
71// If memory leak detection is already enabled, which it is by default, then
72// calling this function has no effect.
73//
74// Note: In multithreaded programs, this function operates on a per-thread
75// basis. In other words, if you call this function from one thread, then
76// memory leak detection is only enabled for that thread. If memory leak
77// detection is disabled for other threads, then it will remain disabled for
78// those other threads. It was designed to work this way to insulate you,
79// the programmer, from having to ensure thread synchronization when calling
80// VLDEnable() and VLDDisable(). Without this, calling these two functions
81// unsynchronized could result in unpredictable and unintended behavior.
82// But this also means that if you want to enable memory leak detection
83// process-wide, then you need to call this function from every thread in
84// the process.
85//
86// Return Value:
87//
88// None.
89//
90__declspec(dllimport) void VLDEnable ();
91
92// VLDRestore - Restore Visual Leak Detector's previous state.
93//
94// Return Value:
95//
96// None.
97//
98__declspec(dllimport) void VLDRestore ();
99
100// VLDGlobalDisable - Disables Visual Leak Detector's memory leak detection at
101// runtime in all threads. If memory leak detection is already disabled,
102// then calling this function has no effect.
103//
104// Return Value:
105//
106// None.
107//
108__declspec(dllimport) void VLDGlobalDisable ();
109
110// VLDGlobalEnable - Enables Visual Leak Detector's memory leak detection
111// at runtime in all threads. If memory leak detection is already enabled,
112// which it is by default, then calling this function has no effect.
113//
114// Return Value:
115//
116// None.
117//
118__declspec(dllimport) void VLDGlobalEnable ();
119
120// VLDReportLeaks - Report leaks up to the execution point.
121//
122// Return Value:
123//
124// None.
125//
126__declspec(dllimport) UINT VLDReportLeaks ();
127
128// VLDGetLeaksCount - Return memory leaks count to the execution point.
129//
130// Return Value:
131//
132// None.
133//
134__declspec(dllimport) UINT VLDGetLeaksCount ();
135
136// VLDMarkAllLeaksAsReported - Mark all leaks as reported.
137//
138// Return Value:
139//
140// None.
141//
142__declspec(dllimport) void VLDMarkAllLeaksAsReported ();
143
144
145// VLDRefreshModules - Look for recently loaded DLLs and patch them if necessary.
146//
147// Return Value:
148//
149// None.
150//
151__declspec(dllimport) void VLDRefreshModules();
152
153
154// VLDEnableModule - Enable Memory leak checking on the specified module.
155//
156// module: module handle.
157//
158// Return Value:
159//
160// None.
161//
162
163__declspec(dllimport) void VLDEnableModule(HMODULE module);
164
165
166// VLDDisableModule - Disable Memory leak checking on the specified module.
167//
168// module: module handle.
169//
170// Return Value:
171//
172// None.
173//
174__declspec(dllimport) void VLDDisableModule(HMODULE module);
175
176// VLDGetOptions - Return all current options.
177//
178// Return Value:
179//
180// Mask of current options.
181//
182__declspec(dllimport) UINT VLDGetOptions();
183
184// VLDGetReportFilename - Return current report filename.
185//
186// filename: current report filename (max characters - MAX_PATH).
187//
188// Return Value:
189//
190// None.
191//
192__declspec(dllimport) void VLDGetReportFilename(WCHAR *filename);
193
194// VLDSetOptions - Update the report options via function call rather than INI file.
195//
196// option_mask: Only the following flags are checked
197// VLD_OPT_AGGREGATE_DUPLICATES
198// VLD_OPT_MODULE_LIST_INCLUDE
199// VLD_OPT_SAFE_STACK_WALK
200// VLD_OPT_SLOW_DEBUGGER_DUMP
201// VLD_OPT_TRACE_INTERNAL_FRAMES
202// VLD_OPT_START_DISABLED
203// VLD_OPT_SKIP_HEAPFREE_LEAKS
204// VLD_OPT_VALIDATE_HEAPFREE
205//
206// maxDataDump: maximum number of user-data bytes to dump for each leaked block.
207//
208// maxTraceFrames: maximum number of frames per stack trace for each leaked block.
209//
210// Return Value:
211//
212// None.
213//
214__declspec(dllimport) void VLDSetOptions(UINT option_mask, SIZE_T maxDataDump, UINT maxTraceFrames);
215
216// VLDSetModulesList - Set list of modules included/excluded in leak detection
217// depending on parameter "includeModules".
218//
219// modules: list of modules to be forcefully included/excluded in leak detection.
220//
221// includeModules: include or exclude that modules.
222//
223// Return Value:
224//
225// None.
226//
227__declspec(dllimport) void VLDSetModulesList(CONST WCHAR *modules, BOOL includeModules);
228
229// VLDGetModulesList - Return current list of included/excluded modules
230// depending on flag VLD_OPT_TRACE_INTERNAL_FRAMES.
231//
232// modules: destination string for list of included/excluded modules (maximum length 512 characters).
233//
234// size: maximum string size.
235//
236// Return Value:
237//
238// BOOL: TRUE if include modules, otherwise FALSE.
239//
240__declspec(dllimport) BOOL VLDGetModulesList(WCHAR *modules, UINT size);
241
242// VLDSetReportOptions - Update the report options via function call rather than INI file.
243//
244// Only the following flags are checked
245// VLD_OPT_REPORT_TO_DEBUGGER
246// VLD_OPT_REPORT_TO_FILE
247// VLD_OPT_REPORT_TO_STDOUT
248// VLD_OPT_UNICODE_REPORT
249//
250// filename is optional and can be NULL.
251//
252// Return Value:
253//
254// None.
255//
256__declspec(dllimport) void VLDSetReportOptions(UINT option_mask, CONST WCHAR *filename);
257
258// VLDSetReportHook - Installs or uninstalls a client-defined reporting function by hooking it
259// into the C run-time debug reporting process (debug version only).
260//
261// mode: The action to take: VLD_RPTHOOK_INSTALL or VLD_RPTHOOK_REMOVE.
262//
263// pfnNewHook: Report hook to install or remove.
264//
265// Return Value:
266//
267// int: 0 if success.
268//
269__declspec(dllimport) int VLDSetReportHook(int mode, VLD_REPORT_HOOK pfnNewHook);
270
271// VLDResolveCallstacks - Performs symbol resolution for all saved extent CallStack's that have
272// been tracked by Visual Leak Detector. This function is necessary for applications that
273// dynamically load and unload modules, and through which memory leaks might be included.
274// If this is NOT called, stack traces may have stack frames with no symbol information. This
275// happens because the symbol API's cannot look up symbols for a binary / module that has been unloaded
276// from the process.
277//
278// Return Value:
279//
280// None.
281//
282__declspec(dllexport) void VLDResolveCallstacks();
283
284#ifdef __cplusplus
285}
286#endif // __cplusplus
287
288#else // !_DEBUG
289
290#define VLDEnable()
291#define VLDDisable()
292#define VLDRestore()
293#define VLDReportLeaks() 0
294#define VLDGetLeaksCount() 0
295#define VLDMarkAllLeaksAsReported()
296#define VLDRefreshModules()
297#define VLDEnableModule(a)
298#define VLDDisableModule(b)
299#define VLDGetOptions() 0
300#define VLDGetReportFilename(a)
301#define VLDSetOptions(a, b, c)
302#define VLDSetReportHook(a, b)
303#define VLDSetModulesList(a)
304#define VLDGetModulesList(a, b) FALSE
305#define VLDSetReportOptions(a, b)
306
307#endif // _DEBUG
308
这 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的条件编译。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53 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
14class MyTest
15{
16public:
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 }
28protected:
29 char *m_pszName;
30};
31
32
33
34int _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}
53
运行效果如图:(为了显示全部,去掉了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