本方法不使用BoundsChecker,Purify等工具
定义DebugHeap类
DebugHeap.h内容如下
------------开始
#pragma once
#include <CRTDBG.H>
void DebugHeap_DllMain(HINSTANCE hInstance, DWORD dwReason, LPVOID lpReserved);
#ifdef _DEBUG
#define DEBUG_NEW new(_NORMAL_BLOCK, THIS_FILE, __LINE__)
#define DEBUG_MALLOC(size) _malloc_dbg(size, _NORMAL_BLOCK, THIS_FILE, __LINE__)
#define strdup(str) _strdup_dbg(str)
#define wcsdup(str) _wcsdup_dbg(str)
#undef THIS_FILE
#else
#define DEBUG_NEW new
#define DEBUG_MALLOC malloc
#endif
#ifdef _DEBUG
#define __new new(_NORMAL_BLOCK, __FILE__, __LINE__)
#define __malloc(size) _malloc_dbg(size, _NORMAL_BLOCK, __FILE__, __LINE__)
#define __free(o) _free_dbg(o, _NORMAL_BLOCK)
#else
#define __new new
#define __malloc(size) malloc(size)
#define __free(o) free(o)
#endif
------------结束
DebugHeap.cpp内容如下
------------开始
#include "stdafx.h"
#include "DebugHeap.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#define malloc DEBUG_MALLOC
static char THIS_FILE[] = __FILE__;
#endif
void DebugHeap_DllMain(HINSTANCE hInstance, DWORD dwReason, LPVOID lpReserved)
{
#ifdef _DEBUG
static _CrtMemState MemCheckpoint;
switch(dwReason)
{
case DLL_PROCESS_ATTACH:
_CrtMemCheckpoint( &MemCheckpoint );
break;
case DLL_PROCESS_DETACH: _CrtMemDumpAllObjectsSince( &MemCheckpoint );
break;
}
#endif
}
------------结束
定义#include "AtlDebugHeap.h"
------------开始
#pragma once
#include "DebugHeap.h"
#ifdef _DEBUG
#define DECLARE_AGGREGATABLE_DBG(x) DECLARE_AGGREGATABLE_DBG2(x, __FILE__, __LINE__)
#define DECLARE_NOT_AGGREGATABLE_DBG(x) DECLARE_NOT_AGGREGATABLE_DBG2(x, __FILE__, __LINE__)
#define CComObjectDBG CComObject_DBG2
#else
#define DECLARE_AGGREGATABLE_DBG(x) DECLARE_AGGREGATABLE(x)
#define DECLARE_NOT_AGGREGATABLE_DBG(x) DECLARE_NOT_AGGREGATABLE(x)
#define CComObjectDBG CComObject
#endif
template <class BASE> class CComObject_DBG2
{
public:
static HRESULT WINAPI CreateInstance(CComObject<BASE>** pp)
{
ATLASSERT(pp != NULL);
HRESULT hRes = E_OUTOFMEMORY;
CComObject<BASE>* p = NULL;
const char* ProgID = BASE::_GetFileName();
int LineNo = BASE::_GetLineNo();
ATLTRY(p = new(_NORMAL_BLOCK, ProgID, LineNo) CComObject<BASE>())
if (p != NULL)
{
p->SetVoid(NULL);
p->InternalFinalConstructAddRef();
hRes = p->FinalConstruct();
p->InternalFinalConstructRelease();
if (hRes != S_OK)
{
delete p;
p = NULL;
}
}
*pp = p;
return hRes;
}
};
template <class T1, class BASE> class CComCreator_DBG
{
public:
static HRESULT WINAPI CreateInstance(void* pv, REFIID riid, LPVOID* ppv)
{
ATLASSERT(*ppv == NULL);
HRESULT hRes = E_OUTOFMEMORY;
T1* p = NULL;
const char* ProgID = BASE::_GetFileName();
int LineNo = BASE::_GetLineNo();
ATLTRY(p = new(_NORMAL_BLOCK, ProgID, LineNo) T1(pv))
if (p != NULL)
{
p->SetVoid(pv);
p->InternalFinalConstructAddRef();
hRes = p->FinalConstruct();
p->InternalFinalConstructRelease();
if (hRes == S_OK)
hRes = p->QueryInterface(riid, ppv);
if (hRes != S_OK)
delete p;
}
return hRes;
}
};
#define DECLARE_AGGREGATABLE_DBG2(x, _aStr_, _LineNo_) public:/
static inline const char* _GetFileName() { return _aStr_; } /
static inline int _GetLineNo() { return _LineNo_; } /
typedef CComCreator2< CComCreator_DBG< CComObject< x >, x >, CComCreator_DBG< CComAggObject< x >, x > > _CreatorClass;
#define DECLARE_NOT_AGGREGATABLE_DBG2(x, _aStr_, _LineNo_) public:/
static inline const char* _GetFileName() { return _aStr_; } /
static inline int _GetLineNo() { return _LineNo_; } /
typedef CComCreator2< CComCreator_DBG< CComObject< x >, x >, CComFailCreator<CLASS_E_NOAGGREGATION> > _CreatorClass;
------------结束
一 在包含DllMain文件中添加步骤:
1:#include "AtlDebugHeap.h"
2:在DllMain函数中添加代码DebugHeap_DllMain(hInstance, dwReason, lpReserved);
二 在某一需要跟踪内存泄露的com对象如CTest(接口为ITest)添加代码如下
1:在CTest.h文件中 #include "AtlDebugHeap.h"
开启跟踪 添加宏DECLARE_AGGREGATABLE_DBG(CTest)
关闭跟踪 添加宏DECLARE_NOT_AGGREGATABLE_DBG(CTest)
2: 在CTest.cpp文件中 添加
#ifdef _DEBUG
#define new DEBUG_NEW
#define malloc DEBUG_MALLOC
static char THIS_FILE[] = __FILE__;
#endif