看流星社区

 找回密码
 注册账号
查看: 1912|回复: 0

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

[复制链接]

该用户从未签到

发表于 2013-4-9 08:50:57 | 显示全部楼层 |阅读模式
#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);
}
点击按钮快速添加回复内容: 支持 高兴 激动 给力 加油 苦寻 生气 回帖 路过 感恩
您需要登录后才可以回帖 登录 | 注册账号

本版积分规则

小黑屋|手机版|Archiver|看流星社区 |网站地图

GMT+8, 2024-4-30 13:31

Powered by Kanliuxing X3.4

© 2010-2019 kanliuxing.com

快速回复 返回顶部 返回列表