首页
社区
课程
招聘
简单实现内存管理方便自己检查内存泄露
发表于: 2012-11-3 18:07 3329

简单实现内存管理方便自己检查内存泄露

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);
}


[注意]传递专业知识、拓宽行业人脉——看雪讲师团队等你加入!

收藏
免费 0
支持
分享
最新回复 (1)
雪    币: 967
活跃值: (1138)
能力值: ( LV6,RANK:90 )
在线值:
发帖
回帖
粉丝
2
奇怪论坛 怎么过滤了 “b f  l  g”
2012-11-3 18:19
0
游客
登录 | 注册 方可回帖
返回
//