debug_new

2472 단어 Code
/************************************************************************/   
/* comment:      debug_new.h    ,                   */   
/*             VC++      (  Intel C++,          )   */   
/*   :         http://blog.vckbase.com/bruceteen/                  */   
/*     :  ,      ,                                       */   
/************************************************************************/   
   
//#include "debug_new.h"    
   
#ifdef _DEBUG    
   
#include     
   
#define CRTDBG_MAP_ALLOC    
#include     
#include     
   
   
class _CriSec   
{   
    CRITICAL_SECTION criSection;   
public:   
    _CriSec()    { InitializeCriticalSection( &criSection ); }   
    ~_CriSec()   { DeleteCriticalSection( &criSection );     }   
    void Enter() { EnterCriticalSection( &criSection );      }   
    void Leave() { LeaveCriticalSection( &criSection );      }   
} _cs;   
   
void _RegDebugNew( void )   
{   
    //_CrtSetDbgFlag( _CRTDBG_REPORT_FLAG | _CRTDBG_LEAK_CHECK_DF );    
    _CrtSetDbgFlag (_CRTDBG_ALLOC_MEM_DF | _CRTDBG_LEAK_CHECK_DF );   
}   
void* __cdecl operator new( size_t nSize, const char* lpszFileName, int nLine )   
{   
    // comment 1: MFC    debug new     ,                     
    //                          ,               .    
    // comment 2: debug new debug delete             ,    ,       
    //                   .    
    // comment 3:   C++    , operator new       set_new_handler       
    //              ,  MSDN   "   new  set_new_handler stub ,        
    //                new.h  _set_new_handler",          .    
    //               VC++6.0  set_new_handler  :    
    //                new_handler __cdecl set_new_handler( new_handler new_p )    
    //                {    
    //                    assert( new_p == 0 ); // cannot use stub to register a new handler    
    //                    _set_new_handler( 0 );    
    //                    return 0;    
    //                }    
    //                    ,    set_new_handler   .    
   
    _cs.Enter();   
    void* p = _malloc_dbg( nSize, _NORMAL_BLOCK, lpszFileName, nLine );   
    _cs.Leave();   
    return p;   
}   
void __cdecl operator delete( void* p, const char* /*lpszFileName*/, int /*nLine*/ )   
{   
    _cs.Enter();   
    _free_dbg( p, _CLIENT_BLOCK );   
    _cs.Leave();   
}   
   
#endif 

좋은 웹페이지 즐겨찾기