VC6调试 ATL 显示内存泄漏代码文件行号

本方法不使用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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值