将以下代码保存为.h文件,在待检测的应用中调用它。
1 /***************************************************************** 2 为了使用内存检测,需要在待检测代码中进行以下三步: 3 1. Define _DEBUG #define _DEBUG 4 2. Include "crtdbg.h" #include "crtdbg.h" 5 3. Let your first line in the code be: _CrtSetDbgFlag (ON); 6 ********************************************************************/ 7 #ifndef _CRTDBG_HEADER 8 #define _CRTDBG_HEADER 9 #ifdef _DEBUG 10 11 extern "C" void WINAPIV NKDbgPrintfW(LPCWSTR lpszFmt, ...); 12 struct CrtFileName 13 { 14 unsigned short* _pusCrtName; 15 CrtFileName* _pCrtNext; 16 }; 17 18 struct _CrtMem 19 { 20 CrtFileName* _pCrtFileName; 21 int _iCrtLine; 22 unsigned int _uiCrtMemLen; 23 void* _pCrtMemAddr; 24 _CrtMem* _pCrtNext; 25 }; 26 27 void* operator new(unsigned int s,unsigned short* name,int line); 28 inline void* __cdecl operator new(unsigned int s) 29 { return ::operator new(s, _T(__FILE__), __LINE__); } 30 31 void __cdecl operator delete(void *pvMem); 32 33 class garbageCollector 34 { 35 public: 36 garbageCollector () {} 37 ~garbageCollector (); 38 }; 39 #define _CrtSetDbgFlag(ignore) garbageCollector gb; 40 _CrtMem* _pCrtMemRoot = 0; 41 CrtFileName* _pCrtFileNameRoot = 0; 42 void* operator new(unsigned int s,unsigned short* name,int line) 43 { 44 void* retPtr = malloc (s); 45 if (retPtr) 46 { _CrtMem* _crtMemCell = (struct _CrtMem*)malloc (sizeof(_CrtMem)); 47 _crtMemCell->_iCrtLine = line; 48 _crtMemCell->_uiCrtMemLen = s; 49 _crtMemCell->_pCrtMemAddr = retPtr; 50 _crtMemCell->_pCrtNext = 0; CrtFileName* _tmpCrtFileName; 51 for (_tmpCrtFileName = _pCrtFileNameRoot; 52 _tmpCrtFileName && wcscmp(name, _tmpCrtFileName->_pusCrtName); 53 _tmpCrtFileName = _tmpCrtFileName->_pCrtNext) {} 54 55 if (!_tmpCrtFileName) 56 { unsigned short* _crtName = (unsigned short*)malloc ((wcslen (name) + 1) * sizeof(unsigned short)); 57 wcscpy (_crtName, name); CrtFileName* _crtFileName = (struct CrtFileName*)malloc (sizeof (CrtFileName)); 58 _crtFileName->_pusCrtName = _crtName; 59 _crtFileName->_pCrtNext = 0; if (!_pCrtFileNameRoot) _pCrtFileNameRoot = _crtFileName; 60 else { for (_tmpCrtFileName = _pCrtFileNameRoot; 61 _tmpCrtFileName->_pCrtNext; 62 _tmpCrtFileName = _tmpCrtFileName->_pCrtNext); 63 _tmpCrtFileName->_pCrtNext = _crtFileName; 64 } 65 _tmpCrtFileName = _crtFileName; 66 } 67 _crtMemCell->_pCrtFileName = _tmpCrtFileName; 68 if (!_pCrtMemRoot) { _pCrtMemRoot = _crtMemCell; } 69 else { _CrtMem* _tmpMemPtr; for (_tmpMemPtr = _pCrtMemRoot; _tmpMemPtr->_pCrtNext; _tmpMemPtr = _tmpMemPtr->_pCrtNext); _tmpMemPtr->_pCrtNext = _crtMemCell; 70 } 71 } 72 return retPtr; 73 } 74 75 void __cdecl operator delete(void *pvMem) 76 { if (pvMem) 77 { _CrtMem* _tmpMem; 78 if (pvMem == _pCrtMemRoot->_pCrtMemAddr) 79 { _tmpMem = _pCrtMemRoot; _pCrtMemRoot = _pCrtMemRoot->_pCrtNext; free (_tmpMem); 80 } 81 else 82 { for (_tmpMem = _pCrtMemRoot; _tmpMem->_pCrtNext && (_tmpMem->_pCrtNext->_pCrtMemAddr != pvMem); 83 _tmpMem = _tmpMem->_pCrtNext); 84 if (_tmpMem->_pCrtNext) 85 { _CrtMem* _tmpMem2; 86 _tmpMem2 = _tmpMem->_pCrtNext; 87 _tmpMem->_pCrtNext = _tmpMem2->_pCrtNext; 88 free (_tmpMem2); } 89 else 90 NKDbgPrintfW (_T("%s(%i) : Warning : deletes memory pointer not allocated with new!/n"), _T(__FILE__), __LINE__); 91 } 92 free (pvMem); 93 } 94 } 95 96 garbageCollector::~garbageCollector () 97 { if (!_pCrtMemRoot) NKDbgPrintfW (_T("No memory leaks detected!/n")); 98 else { _CrtMem* _tmpMem; NKDbgPrintfW (_T("Detected memory leaks!/nDumping objects ->/n")); 99 for (_tmpMem = _pCrtMemRoot; _tmpMem; _tmpMem = _tmpMem->_pCrtNext) 100 { 101 NKDbgPrintfW (_T("%s(%i) : normal block at 0x%08X, %i bytes long/n Data <"), _tmpMem->_pCrtFileName->_pusCrtName, _tmpMem->_iCrtLine, _tmpMem->_pCrtMemAddr, _tmpMem->_uiCrtMemLen); 102 103 for (unsigned int i = 0; i < _tmpMem->_uiCrtMemLen; i++) 104 NKDbgPrintfW (_T("%c"), *(((char*)_tmpMem->_pCrtMemAddr)+i)); NKDbgPrintfW (_T(">/n")); 105 } 106 } 107 CrtFileName* _tmpName = _pCrtFileNameRoot; 108 for (;_tmpName;) 109 { _pCrtFileNameRoot = _tmpName->_pCrtNext; free(_tmpName->_pusCrtName); free(_tmpName); _tmpName = _pCrtFileNameRoot; 110 } 111 } #else 112 #define _CrtSetDbgFlag(ignore) #endif //DEBUG #endif //HEADER