//debug_new.h///Begin
#ifndef _DEBUG_NEW_H_
#define _DEBUG_NEW_H_
#ifdef _DEBUG
#undef new
extern void _RegDebugNew( void );
extern void* __cdecl operator new( size_t, const char*, int );
extern void __cdecl operator delete( void*, const char*, int);
#define new new(__FILE__, __LINE__)
#define REG_DEBUG_NEW _RegDebugNew();
#else
#define REG_DEBUG_NEW
#endif // _DEBUG
#endif // _DEBUG_NEW_H_
#include "stdafx.h"
#include <windows.h>
#include <crtdbg.h>
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 );
}
void * __cdecl operator new( size_t nSize, const char* lpszFileName, int nLine )
{
_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();
}
1>------ Build started: Project: Huang, Configuration: Debug Win32 ------
1>Compiling...
1>debug_new.cpp
1>c:\temp\huang\huang\debug_new.cpp(20) : warning C4229: anachronism used : modifiers on data are ignored
1>c:\temp\huang\huang\debug_new.cpp(20) : error C2365: 'operator new' : redefinition; previous definition was 'function'
1>c:\temp\huang\huang\debug_new.cpp(20) : error C2078: too many initializers
1>c:\temp\huang\huang\debug_new.cpp(20) : error C2440: 'initializing' : cannot convert from 'int' to 'void *'
1> Conversion from integral type to pointer type requires reinterpret_cast, C-style cast or function-style cast
1>c:\temp\huang\huang\debug_new.cpp(20) : error C2143: syntax error : missing ';' before '('
1>c:\temp\huang\huang\debug_new.cpp(20) : error C2226: syntax error : unexpected type 'size_t'
1>c:\temp\huang\huang\debug_new.cpp(20) : error C2059: syntax error : ')'
1>c:\temp\huang\huang\debug_new.cpp(21) : error C2143: syntax error : missing ';' before '{'
1>c:\temp\huang\huang\debug_new.cpp(21) : error C2447: '{' : missing function header (old-style formal list?)
1>HuangView.cpp
1>c:\temp\huang\huang\huangview.cpp(12) : warning C4005: 'new' : macro redefinition
1> c:\temp\huang\huang\debug_new.h(10) : see previous definition of 'new'
1>Generating Code...
1>Build log was saved at "file://c:\Temp\Huang\Huang\Debug\BuildLog.htm"
1>Huang - 8 error(s), 2 warning(s)
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========
希望有谁可以帮我解决这个问题,或者帮我写个重载全局的你new 函数也行。