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