-
Notifications
You must be signed in to change notification settings - Fork 141
Expand file tree
/
Copy pathMemLeakDetect.cpp
More file actions
532 lines (477 loc) · 13.8 KB
/
MemLeakDetect.cpp
File metadata and controls
532 lines (477 loc) · 13.8 KB
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
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
/*************************************************************
Author : David A. Jones
File Name : MemLeakDetect.h
Date : July 30, 2004
Synopsis
A trace memory feature for source code to trace and
find memory related bugs.
****************************************************************/
#define VC_EXTRALEAN // Exclude rarely-used stuff from Windows headers
//
#pragma warning(disable:4312)
#pragma warning(disable:4313)
#pragma warning(disable:4267)
#pragma warning(disable:4100)
#include "stdafx.h"
#include "MemLeakDetect.h"
#ifdef _DEBUG
static CMemLeakDetect* g_pMemTrace = NULL;
static _CRT_ALLOC_HOOK pfnOldCrtAllocHook = NULL;
extern int _crtDgbFlag;
int MyTrace(LPCTSTR lpszFormat, ...)
{
TCHAR buffer[1024];
va_list args;
va_start( args, lpszFormat);
vsprintf( buffer, lpszFormat, args );
return _CrtDbgReport(_CRT_WARN,NULL,NULL,NULL,buffer);
}
int catchMemoryAllocHook(int allocType,
void *userData,
size_t size,
int blockType,
long requestNumber,
const unsigned char *filename,
int lineNumber)
{
_CrtMemBlockHeader *pCrtHead;
long prevRequestNumber;
// internal C library internal allocations
if ( blockType == _CRT_BLOCK || gMemLeakDetect.stopped)
{
return( TRUE );
}
// check if someone has turned off mem tracing
if ((( _CRTDBG_ALLOC_MEM_DF & _crtDbgFlag) == 0) &&
(( allocType == _HOOK_ALLOC) ||
( allocType == _HOOK_REALLOC)))
{
if (pfnOldCrtAllocHook)
{
pfnOldCrtAllocHook(allocType, userData, size, blockType, requestNumber, filename, lineNumber);
}
return TRUE;
}
// protect if mem trace is not initialized
if (g_pMemTrace == NULL)
{
if (pfnOldCrtAllocHook)
{
pfnOldCrtAllocHook(allocType, userData, size, blockType, requestNumber, filename, lineNumber);
}
return TRUE;
}
// protect internal mem trace allocs
if (g_pMemTrace->isLocked)
{
if (pfnOldCrtAllocHook)
{
pfnOldCrtAllocHook(allocType, userData, size, blockType, requestNumber, filename, lineNumber);
}
return( TRUE);
}
// lock the function
g_pMemTrace->isLocked = true;
//
if (allocType == _HOOK_ALLOC)
{
g_pMemTrace->addMemoryTrace((void *) requestNumber, size, (char*)filename, lineNumber);
}
else
if (allocType == _HOOK_REALLOC)
{
if (_CrtIsValidHeapPointer(userData))
{
pCrtHead = pHdr(userData);
prevRequestNumber = pCrtHead->lRequest;
//
if (pCrtHead->nBlockUse == _IGNORE_BLOCK)
{
if (pfnOldCrtAllocHook)
{
pfnOldCrtAllocHook(allocType, userData, size, blockType, requestNumber, filename, lineNumber);
}
goto END;
}
g_pMemTrace->redoMemoryTrace((void *) requestNumber, (void *) prevRequestNumber, size, (char*)filename, lineNumber);
}
}
else
if (allocType == _HOOK_FREE)
{
if (_CrtIsValidHeapPointer(userData))
{
pCrtHead = pHdr(userData);
requestNumber = pCrtHead->lRequest;
//
if (pCrtHead->nBlockUse == _IGNORE_BLOCK)
{
if (pfnOldCrtAllocHook)
{
pfnOldCrtAllocHook(allocType, userData, size, blockType, requestNumber, filename, lineNumber);
}
goto END;
}
g_pMemTrace->removeMemoryTrace((void *) requestNumber, userData);
}
}
END:
// unlock the function
g_pMemTrace->isLocked = false;
return TRUE;
}
void CMemLeakDetect::addMemoryTrace(void* addr, DWORD asize, TCHAR *fname, DWORD lnum)
{
if (this->stopped)
return;
AllocBlockInfo ainfo;
//
if (m_AllocatedMemoryList.Lookup(addr, ainfo))
{
// already allocated
AfxTrace("ERROR!CMemLeakDetect::addMemoryTrace() Address(0x%08X) already allocated\n", addr);
return;
}
//
ainfo.address = addr;
ainfo.lineNumber = lnum;
ainfo.size = asize;
ainfo.occurance = memoccurance++;
MLD_STACKWALKER(&ainfo.traceinfo[0]);
//
if (fname)
_tcsncpy(&ainfo.fileName[0], fname, MLD_MAX_NAME_LENGTH);
else
ainfo.fileName[0] = 0;
//
m_AllocatedMemoryList.SetAt(addr, ainfo);
};
void CMemLeakDetect::redoMemoryTrace(void* addr, void* oldaddr, DWORD asize, char *fname, DWORD lnum)
{
if (this->stopped)
return;
AllocBlockInfo ainfo;
if (m_AllocatedMemoryList.Lookup(oldaddr,(AllocBlockInfo &) ainfo))
{
m_AllocatedMemoryList.RemoveKey(oldaddr);
}
else
{
AfxTrace(_T("ERROR!CMemLeakDetect::redoMemoryTrace() didnt find Address(0x%08X) to free\n"), oldaddr);
}
//
ainfo.address = addr;
ainfo.lineNumber = lnum;
ainfo.size = asize;
ainfo.occurance = memoccurance++;
MLD_STACKWALKER(&ainfo.traceinfo[0]);
//
if (fname)
_tcsncpy(&ainfo.fileName[0], fname, MLD_MAX_NAME_LENGTH);
else
ainfo.fileName[0] = 0;
m_AllocatedMemoryList.SetAt(addr, ainfo);
};
void CMemLeakDetect::removeMemoryTrace(void* addr, void* realdataptr)
{
AllocBlockInfo ainfo;
//
if (m_AllocatedMemoryList.Lookup(addr,(AllocBlockInfo &) ainfo))
{
m_AllocatedMemoryList.RemoveKey(addr);
}
else
{
//freeing unallocated memory
//AfxTrace(_T("ERROR!CMemLeakDetect::removeMemoryTrace() didnt find Address(0x%08X) to free\n"), addr);
}
};
void CMemLeakDetect::cleanupMemoryTrace()
{
m_AllocatedMemoryList.RemoveAll();
};
void CMemLeakDetect::dumpMemoryTrace()
{
POSITION pos;
LPVOID addr;
AllocBlockInfo ainfo;
TCHAR buf[MLD_MAX_NAME_LENGTH];
TCHAR symInfo[MLD_MAX_NAME_LENGTH];
TCHAR srcInfo[MLD_MAX_NAME_LENGTH];
int totalSize = 0;
int numLeaks = 0;
STACKFRAMEENTRY* p = 0;
//
_tcscpy(symInfo, MLD_TRACEINFO_NOSYMBOL);
_tcscpy(srcInfo, MLD_TRACEINFO_NOSYMBOL);
//
pos = m_AllocatedMemoryList.GetStartPosition();
//
while(pos != m_AllocatedMemoryList.end())
{
numLeaks++;
sprintf(buf, "Memory Leak(%d)------------------->\n", numLeaks);
AfxTrace(buf);
//
m_AllocatedMemoryList.GetNextAssoc(pos, (LPVOID &) addr, (AllocBlockInfo&) ainfo);
if (ainfo.fileName[0] != NULL)
{
sprintf(buf, "Memory Leak <0x%X> bytes(%d) occurance(%d) %s(%d)\n",
ainfo.address, ainfo.size, ainfo.occurance, ainfo.fileName, ainfo.lineNumber);
}
else
{
sprintf(buf, "Memory Leak <0x%X> bytes(%d) occurance(%d)\n",
ainfo.address, ainfo.size, ainfo.occurance);
}
//
AfxTrace(buf);
//
p = &ainfo.traceinfo[0];
while(p[0].addrPC.Offset)
{
symFunctionInfoFromAddresses( p[0].addrPC.Offset, p[0].addrFrame.Offset, symInfo );
symSourceInfoFromAddress( p[0].addrPC.Offset, srcInfo );
AfxTrace("%s->%s()\n", srcInfo, symInfo);
p++;
}
totalSize += ainfo.size;
}
_stprintf(buf, _T("\n-----------------------------------------------------------\n"));
AfxTrace(buf);
if(!totalSize)
{
_stprintf(buf,_T("No Memory Leaks Detected for %d Allocations\n\n"), memoccurance);
AfxTrace(buf);
}
else
{
_stprintf(buf, _T("Total %d Memory Leaks: %d bytes Total Alocations %d\n\n"), numLeaks, totalSize, memoccurance);
AfxTrace(buf);
}
}
void CMemLeakDetect::Init()
{
m_dwsymBufSize = (MLD_MAX_NAME_LENGTH + sizeof(PIMAGEHLP_SYMBOL));
m_hProcess = GetCurrentProcess();
m_pSymbol = (PIMAGEHLP_SYMBOL)GlobalAlloc( GMEM_FIXED, m_dwsymBufSize);
m_AllocatedMemoryList.InitHashTable(10211, TRUE);
initSymInfo( NULL );
isLocked = false;
g_pMemTrace = this;
pfnOldCrtAllocHook = _CrtSetAllocHook( catchMemoryAllocHook );
}
void CMemLeakDetect::End()
{
isLocked = true;
_CrtSetAllocHook(pfnOldCrtAllocHook);
dumpMemoryTrace();
cleanupMemoryTrace();
cleanupSymInfo();
GlobalFree(m_pSymbol);
g_pMemTrace = NULL;
}
CMemLeakDetect::CMemLeakDetect()
{
stopped = false;
Init();
}
CMemLeakDetect::~CMemLeakDetect()
{
End();
}
// PRIVATE STUFF
void CMemLeakDetect::symbolPaths( TCHAR* lpszSymbolPath)
{
TCHAR lpszPath[MLD_MAX_NAME_LENGTH];
// Creating the default path where the dgbhelp.dll is located
// ".;%_NT_SYMBOL_PATH%;%_NT_ALTERNATE_SYMBOL_PATH%;%SYSTEMROOT%;%SYSTEMROOT%\System32;"
_tcscpy( lpszSymbolPath, _T(".;..\\;..\\..\\"));
// environment variable _NT_SYMBOL_PATH
if ( GetEnvironmentVariable(_T("_NT_SYMBOL_PATH"), lpszPath, MLD_MAX_NAME_LENGTH ))
{
strcat( lpszSymbolPath, _T(";"));
strcat( lpszSymbolPath, lpszPath );
}
// environment variable _NT_ALTERNATE_SYMBOL_PATH
if ( GetEnvironmentVariable( _T("_NT_ALTERNATE_SYMBOL_PATH"), lpszPath, MLD_MAX_NAME_LENGTH ))
{
_tcscat( lpszSymbolPath, _T(";"));
_tcscat( lpszSymbolPath, lpszPath );
}
// environment variable SYSTEMROOT
if ( GetEnvironmentVariableA( "SYSTEMROOT", lpszPath, MLD_MAX_NAME_LENGTH ) )
{
_tcscat( lpszSymbolPath, _T(";"));
_tcscat( lpszSymbolPath, lpszPath);
_tcscat( lpszSymbolPath, _T(";"));
// SYSTEMROOT\System32
_tcscat( lpszSymbolPath, lpszPath );
_tcscat( lpszSymbolPath, _T("\\System32"));
}
}
BOOL CMemLeakDetect::cleanupSymInfo()
{
return SymCleanup( GetCurrentProcess() );
}
// Initializes the symbol files
BOOL CMemLeakDetect::initSymInfo( TCHAR* lpszUserSymbolPath )
{
CHAR lpszSymbolPath[MLD_MAX_NAME_LENGTH];
DWORD symOptions = SymGetOptions();
symOptions |= SYMOPT_LOAD_LINES;
symOptions &= ~SYMOPT_UNDNAME;
SymSetOptions( symOptions );
// Get the search path for the symbol files
symbolPaths( lpszSymbolPath);
//
if (lpszUserSymbolPath)
{
_tcscat(lpszSymbolPath, _T(";"));
_tcscat(lpszSymbolPath, lpszUserSymbolPath);
}
return SymInitialize( GetCurrentProcess(), lpszSymbolPath, TRUE);
}
void CMemLeakDetect::symStackTrace(STACKFRAMEENTRY* pStacktrace )
{
STACKFRAME callStack;
BOOL bResult;
CONTEXT context;
HANDLE hThread = GetCurrentThread();
// get the context
memset( &context, NULL, sizeof(context) );
context.ContextFlags = CONTEXT_FULL;
if ( !GetThreadContext( hThread, &context ) )
{
AfxTrace("Call stack info(thread=0x%X) failed.\n", hThread );
return;
}
//initialize the call stack
memset( &callStack, NULL, sizeof(callStack) );
callStack.AddrPC.Offset = context.Eip;
callStack.AddrStack.Offset = context.Esp;
callStack.AddrFrame.Offset = context.Ebp;
callStack.AddrPC.Mode = AddrModeFlat;
callStack.AddrStack.Mode = AddrModeFlat;
callStack.AddrFrame.Mode = AddrModeFlat;
//
for( DWORD index = 0; index < MLD_MAX_TRACEINFO; index++ )
{
bResult = StackWalk(IMAGE_FILE_MACHINE_I386,
m_hProcess,
hThread,
&callStack,
NULL,
NULL,
SymFunctionTableAccess,
SymGetModuleBase,
NULL);
//if ( index == 0 )
// continue;
if( !bResult || callStack.AddrFrame.Offset == 0 )
break;
//
pStacktrace[0].addrPC = callStack.AddrPC;
pStacktrace[0].addrFrame = callStack.AddrFrame;
pStacktrace++;
}
//clear the last entry
memset(pStacktrace, NULL, sizeof(STACKFRAMEENTRY));
}
//
// This code is still under investigation
// I have to test this code and make sure it is compatible
// with the other stack walker!
//
void CMemLeakDetect::symStackTrace2(STACKFRAMEENTRY* pStacktrace )
{
ADDR FramePtr = NULL;
ADDR InstructionPtr = NULL;
ADDR OriFramePtr = NULL;
ADDR PrevFramePtr = NULL;
long StackIndex = NULL;
// Get frame pointer
_asm mov DWORD PTR [OriFramePtr], ebp
FramePtr = OriFramePtr;
//
while (FramePtr)
{
InstructionPtr = ((ADDR *)FramePtr)[1];
pStacktrace[StackIndex].addrPC.Offset = InstructionPtr;
pStacktrace[StackIndex].addrPC.Segment = NULL;
pStacktrace[StackIndex].addrPC.Mode = AddrModeFlat;
//
StackIndex++;
PrevFramePtr = FramePtr;
FramePtr = ((ADDR *)FramePtr)[0];
}
}
BOOL CMemLeakDetect::symFunctionInfoFromAddresses( ULONG fnAddress, ULONG stackAddress, LPTSTR lpszSymbol )
{
DWORD dwDisp = 0;
::ZeroMemory(m_pSymbol, m_dwsymBufSize );
m_pSymbol->SizeOfStruct = m_dwsymBufSize;
m_pSymbol->MaxNameLength = m_dwsymBufSize - sizeof(IMAGEHLP_SYMBOL);
// Set the default to unknown
_tcscpy( lpszSymbol, MLD_TRACEINFO_NOSYMBOL);
// Get symbol info for IP
if ( SymGetSymFromAddr( m_hProcess, (ULONG)fnAddress, &dwDisp, m_pSymbol ) )
{
_tcscpy(lpszSymbol, m_pSymbol->Name);
return TRUE;
}
//create the symbol using the address because we have no symbol
_stprintf(lpszSymbol, "0x%08X", fnAddress);
return FALSE;
}
BOOL CMemLeakDetect::symSourceInfoFromAddress(UINT address, TCHAR* lpszSourceInfo)
{
BOOL ret = FALSE;
IMAGEHLP_LINE lineInfo;
DWORD dwDisp;
TCHAR lpModuleInfo[MLD_MAX_NAME_LENGTH] = MLD_TRACEINFO_EMPTY;
_tcscpy( lpszSourceInfo, MLD_TRACEINFO_NOSYMBOL);
memset( &lineInfo, NULL, sizeof( IMAGEHLP_LINE ) );
lineInfo.SizeOfStruct = sizeof( IMAGEHLP_LINE );
if ( SymGetLineFromAddr( m_hProcess, address, &dwDisp, &lineInfo ) )
{
// Using the "sourcefile(linenumber)" format
_stprintf( lpszSourceInfo, _T("%s(%d): 0x%08X"), lineInfo.FileName, lineInfo.LineNumber, address );
ret = TRUE;
}
else
{
// Using the "modulename!address" format
symModuleNameFromAddress( address, lpModuleInfo );
if ( lpModuleInfo[0] == _T('?') || lpModuleInfo[0] == _T('\0'))
{
// Using the "address" format
_stprintf(lpszSourceInfo, _T("0x%08X"), lpModuleInfo, address );
}
else
{
_stprintf(lpszSourceInfo, _T("%sdll! 0x%08X"), lpModuleInfo, address );
}
ret = FALSE;
}
//
return ret;
}
BOOL CMemLeakDetect::symModuleNameFromAddress( UINT address, TCHAR* lpszModule )
{
BOOL ret = FALSE;
IMAGEHLP_MODULE moduleInfo;
::ZeroMemory( &moduleInfo, sizeof(IMAGEHLP_MODULE) );
moduleInfo.SizeOfStruct = sizeof(IMAGEHLP_MODULE);
if ( SymGetModuleInfo( m_hProcess, (DWORD)address, &moduleInfo ) )
{
_tcscpy(moduleInfo.ModuleName, lpszModule);
ret = TRUE;
}
else
{
_tcscpy( lpszModule, MLD_TRACEINFO_NOSYMBOL);
}
return ret;
}
#endif