//mymemory.h #pragma once #ifndef _MY_MEMORY_H #define _MY_MEMORY_H #include <new> extern void* operator new(size_t size, const char* pszStr, int iLine) throw(std::bad_alloc); extern void operator delete(void* ptr); extern void* operator new[](size_t size, const char* pszStr, int iLine) throw(std::bad_alloc); extern void operator delete[](void* ptr); extern void CheckMemLeak(); extern void StartMemCheck(); extern void MyLock(); extern void MyUnLock(); #endif //mymemory.cpp #include <cstdlib> #include <iostream> #include <Windows.h> #include <map> #include "mymemory.h" typedef struct tagMemMallocInfo { char m_szFile[256]; int m_iLine; } MemMallocInfo; static std::map<void*, MemMallocInfo> g_mapMemMallocInfo; static void* reserveBuf; static void FreeReserveBuf(); static void* MyMalloc(size_t size); static void MyFree(void* ptr); void* MyMalloc(size_t size) { void* ptr = NULL; if (size <= 0) { size = 1; } MyLock(); ptr = malloc(size); MyUnLock(); return ptr; } void MyFree(void * ptr) { MyLock(); free(ptr); MyUnLock(); } void FreeReserveBuf() { if (reserveBuf != NULL) { MyFree(reserveBuf); reserveBuf = NULL; } std::cout << "warning:Out of memory." << std::endl; } void* operator new(size_t size, const char* pszStr, int iLine) { void* pTmp = NULL; if (size <= 0) { size = 1; } while (true) { pTmp = MyMalloc(size); if (NULL == pTmp) { std::new_handler p = std::set_new_handler(NULL); if (p != NULL) { (*p)(); } else { throw std::bad_alloc(); } } else { MemMallocInfo tmp; memset(&tmp, 0, sizeof(MemMallocInfo)); strncpy(tmp.m_szFile, pszStr, sizeof(tmp.m_szFile) - 1); tmp.m_iLine = iLine; g_mapMemMallocInfo.insert(std::map<void*, MemMallocInfo>::value_type(pTmp, tmp)); break; } } return pTmp; } void operator delete(void* ptr) { std::map<void*, MemMallocInfo>::iterator ite = g_mapMemMallocInfo.find(ptr); if (ite != g_mapMemMallocInfo.end()) { g_mapMemMallocInfo.erase(ite); MyFree(ptr); } } void* operator new[](size_t size, const char* pszStr, int iLine) { void* pTmp = NULL; if (size <= 0) { size = 1; } while (true) { pTmp = MyMalloc(size); if (NULL == pTmp) { std::new_handler p = std::set_new_handler(0); if (p != NULL) { (*p)(); } else { throw std::bad_alloc(); } } else { MemMallocInfo tmp; memset(&tmp, 0, sizeof(MemMallocInfo)); strncpy(tmp.m_szFile, pszStr, sizeof(tmp.m_szFile) - 1); tmp.m_iLine = iLine; g_mapMemMallocInfo.insert(std::map<void*, MemMallocInfo>::value_type(pTmp, tmp)); break; } } return pTmp; } void operator delete[](void* ptr) { std::map<void*, MemMallocInfo>::iterator ite = g_mapMemMallocInfo.find(ptr); if (ite != g_mapMemMallocInfo.end()) { g_mapMemMallocInfo.erase(ite); MyFree(ptr); } } void CheckMemLeak() { for (std::map<void*, MemMallocInfo>::iterator ite = g_mapMemMallocInfo.begin(); ite != g_mapMemMallocInfo.end(); ite++) { std::cout << "memory alloc at = file:" << ite->second.m_szFile << " line:" << ite->second.m_iLine << std::endl; } } void StartMemCheck() { reserveBuf = MyMalloc(1024 * 1024 * 4); std::set_new_handler(FreeReserveBuf); } #ifdef _WIN32 static CRITICAL_SECTION g_cs; static bool g_bInitCriticalSec = false; void MyLock() { if (!g_bInitCriticalSec) { InitializeCriticalSection(&g_cs); } EnterCriticalSection(&g_cs); } void MyUnLock() { LeaveCriticalSection(&g_cs); } #elif LINUX void MyLock() { // } void MyUnLock() { // } #endif //使用实例 #include "mymemory.h" #define new new(__FILE__, __LINE__) int _tmain(int argc, _TCHAR* argv[]) { StartMemCheck(); //主体代码 CheckMemLeak(); return 0; }