-
-
简单实现内存管理方便自己检查内存泄露
-
发表于:
2012-11-3 18:07
3329
-
#include <Windows.h>
#include <map>
#include <string>
using namespace std;
//以函数名为主键
//
typedef map<LPVOID,DWORD> MAPAPILIST;
typedef map<LPVOID,DWORD>::iterator ITMAPAPILIST;
typedef map<wstring,MAPAPILIST> MAPMANAGEMEMORY;
typedef map<wstring,MAPAPILIST>::iterator ITMAPMANAGEMEMORY;
class CExManageMemory
{
public:
CExManageMemory();
static CExManageMemory * _Instance;
static CExManageMemory * GetInstance();
static void UnInstall(BOOL IsFree);
void * ExMalloc(LPWSTR lpszApiName,size_t Size );
BOOL FreeAllMemory();
BOOL ExFree(LPWSTR lpszApiName,void* Memory);
~CExManageMemory();
protected:
private:
CRITICAL_SECTION m_cs;
MAPMANAGEMEMORY m_MapManageMemory;
};
#include "ExManageMemory.h"
CExManageMemory * CExManageMemory::_Instance=NULL;
BOOL CExManageMemory::ExFree(LPWSTR lpszApiName,void* Memory)
{
BOOL bResult=FALSE;
ITMAPAPILIST itMapApiList;
ITMAPMANAGEMEMORY itMapManageMemory;
EnterCriticalSection(&m_cs);
itMapManageMemory=m_MapManageMemory.find(lpszApiName);
if (itMapManageMemory!=m_MapManageMemory.end())
{
itMapApiList=itMapManageMemory->second.find(Memory);
if (itMapApiList!=itMapManageMemory->second.end())
{
itMapManageMemory->second.erase(itMapApiList);
free(Memory);
bResult=TRUE;
}
}
LeaveCriticalSection(&m_cs);
return bResult;
}
void * CExManageMemory::ExMalloc(LPWSTR lpszApiName,size_t Size )
{
LPVOID lpszBuffer=NULL;
lpszBuffer=malloc(Size);
if (lpszBuffer)
{
EnterCriticalSection(&m_cs);
m_MapManageMemory[lpszApiName][lpszBuffer]=Size;
LeaveCriticalSection(&m_cs);
}
return lpszBuffer;
}
CExManageMemory::CExManageMemory()
{
InitializeCriticalSection(&m_cs);
}
CExManageMemory::~CExManageMemory()
{
DeleteCriticalSection(&m_cs);
}
CExManageMemory * CExManageMemory::GetInstance()
{
if (!_Instance)
{
_Instance=new CExManageMemory;
}
return _Instance;
}
void CExManageMemory::UnInstall(BOOL IsFree)
{
if (_Instance)
{
if (IsFree)
{
_Instance->FreeAllMemory();
}
delete _Instance;
_Instance=NULL;
}
}
BOOL CExManageMemory::FreeAllMemory()
{
BOOL bResult=FALSE;
ITMAPAPILIST itMapApiList;
ITMAPMANAGEMEMORY itMapManageMemory;
for (itMapManageMemory=m_MapManageMemory.begin();itMapManageMemory!=m_MapManageMemory.end();itMapManageMemory++)
{
for (itMapApiList =itMapManageMemory->second.begin();itMapApiList!=itMapManageMemory->second.end();itMapApiList++)
{
if (itMapApiList->first)
{
free(itMapApiList->first);
}
}
}
bResult=TRUE;
Finally:
return bResult;
}
#include <Windows.h>
#include "ExManageMemory.h"
void Test()
{
LPVOID lpBuffer=NULL;
BOOL bTest=FALSE;
CExManageMemory * Instance=CExManageMemory::GetInstance();
lpBuffer=Instance->ExMalloc(L"Test",5);
lpBuffer=Instance->ExMalloc(L"Test",5);
lpBuffer=Instance->ExMalloc(L"Test",5);
}
void main()
{
LPVOID lpBuffer=NULL;
BOOL bTest=FALSE;
CExManageMemory * Instance=CExManageMemory::GetInstance();
Test();
lpBuffer=Instance->ExMalloc(L"main",5);
lpBuffer=Instance->ExMalloc(L"main",5);
lpBuffer=Instance->ExMalloc(L"main",5);
bTest=Instance->ExFree(L"main",lpBuffer);
Instance->UnInstall(TRUE);
}
[注意]传递专业知识、拓宽行业人脉——看雪讲师团队等你加入!