您能否修改您的代码以使用 和 的调试malloc
版本?如果是,请检查和。realloc
free
_malloc_dbg
_realloc_dbg
_free_dbg
(您可以根据这些函数编写自己的new
和运算符。)delete
#ifdef _DEBUG
# define _CRTDBG_MAP_ALLOC 1
# include <Crtdbg.h>
# define malloc(size) _malloc_dbg(size,_CLIENT_BLOCK,__FILE__,__LINE__)
# define realloc(addr,size) _realloc_dbg(addr,size,_CLIENT_BLOCK,__FILE__,__LINE__)
# define free(addr) _free_dbg(addr,_CLIENT_BLOCK)
void * operator new ( size_t size, const char * filename, int linenumber )
{
void * addr = _malloc_dbg( size, _CLIENT_BLOCK, filename, linenumber );
if ( addr == 0 )
throw std::bad_alloc;
return addr;
}
void * operator new ( size_t size, const std::nothrow_t &no_throw, const char * filename, int linenumber )
{
return _malloc_dbg( size, _CLIENT_BLOCK, filename, linenumber );
}
void * operator new [] ( size_t size, const char * filename, int linenumber )
{
void * addr = _malloc_dbg( size, _CLIENT_BLOCK, filename, linenumber );
if ( addr == 0 )
throw std::bad_alloc;
return addr;
}
void * operator new [] ( size_t size, const std::nothrow_t &no_throw, const char * filename, int linenumber )
{
return _malloc_dbg( size, _CLIENT_BLOCK, filename, linenumber );
}
void operator delete( void *p, const char * filename, int linenumber )
{
_free_dbg(p,_CLIENT_BLOCK);
}
void operator delete [] ( void *p, const char * filename, int linenumber )
{
_free_dbg(p,_CLIENT_BLOCK);
}
# define DEBUG_NEW_HEAP new( __FILE__, __LINE__ )
# define new DEBUG_NEW_HEAP
#endif
(参考:上一个主题)