随笔 - 64, 文章 - 11, 评论 - 12, 引用 - 0
数据加载中……

打造自己的DEBUG_NEW

在使用MFC库开发程序时,我非常喜欢MFC框架中的内存泄漏诊断机制,它的确能很好地帮助我们查找出内存泄漏。可是链接了MFC库也使得生成的可执行文件大了许多,这个没什么负面影响。最可怕的是如果仅为了使用内存诊断机制,而带来了链接库冲突的麻烦。我也是在遇到这个问题时,才写出了一个简易的内存诊断机制。
在windows的SDK中有一套用于诊断内存泄漏的机制,只是MFC封装得更好,所以知名度不够高。下面我封装了CRT中的内存泄漏诊断机制,用起来也比较方便,直接在stdafx.h中包含MyDebug.h, 在要用诊断机制的文件中加入如下的代码。
#ifdef _DEBUG
#define new DEBUG_NEW
#endif
/*
* @File: DebugNew.h
*
* @Author: Robert xiao
*
* @Created: 2012/11/04
*
* @Purpose:    
*/


#pragma once

#define THIS_FILE    __FILE__
void* __cdecl operator new(size_t nSize, int nType, const char* lpszFileName, int nLine);

#ifdef _DEBUG
void* _cdecl operator new(size_t nSize, const char* lpszFileName, int nLine);
void* __cdecl operator new[](size_t nSize, const char* lpszFileName, int nLine);
void __cdecl operator delete(void* p);
void __cdecl operator delete[](void* p);
#define DEBUG_NEW new(THIS_FILE, __LINE__)
#else
#define DEBUG_NEW new
#endif // _DEBUG

/*
* @File: DebugNew.cpp
*
* @Author: Robert xiao
*
* @Created: 2012/11/04
*
* @Purpose:    
*
*/

#include 
"stdafx.h"
#include 
<crtdbg.h>
#include 
"MyDebug.h"

namespace
{
    
class AutoDetectMemory
    
{
    
public:
        AutoDetectMemory()
        
{
#ifdef _DEBUG
            _CrtSetReportMode( _CRT_ERROR, _CRTDBG_MODE_DEBUG );
            _CrtSetDbgFlag ( _CRTDBG_ALLOC_MEM_DF 
| _CRTDBG_LEAK_CHECK_DF );
#endif
        }

    }
;

    
static AutoDetectMemory gs_am;
}



void* _cdecl operator new(size_t nSize, const char* lpszFileName, int nLine)
{
    
return ::operator new(nSize, _NORMAL_BLOCK, lpszFileName, nLine);
}


void* __cdecl operator new[](size_t nSize, const char* lpszFileName, int nLine)
{
    
return ::operator new(nSize, _NORMAL_BLOCK, lpszFileName, nLine);
}


void __cdecl operator delete(void* p)
{
#if defined(_DEBUG)
    _free_dbg(p, _NORMAL_BLOCK);
#else
    free(p);
#endif
}


void __cdecl operator delete[](void* p)
{
    ::
operator delete(p);
}


void* __cdecl operator new(size_t nSize, int nType, const char* lpszFileName, int nLine)
{
#ifndef _DEBUG
    UNREFERENCED_PARAMETER(nType);
    UNREFERENCED_PARAMETER(lpszFileName);
    UNREFERENCED_PARAMETER(nLine);
    
return ::operator new(nSize);
#else
    
return _malloc_dbg(nSize, nType, lpszFileName, nLine);
#endif
}

posted on 2012-11-05 11:53 Robertxiao 阅读(1730) 评论(0)  编辑 收藏 引用 所属分类: Windows/MFC探索


只有注册用户登录后才能发表评论。
网站导航: 博客园   IT新闻   BlogJava   知识库   博问   管理