Could you modify your code to use the debug version of malloc
, realloc
and free
? If yes, check _malloc_dbg, _realloc_dbg and _free_dbg.
(You could write own new
and delete
operators based on these functions.)
#ifdef _DEBUG
# define _CRTDBG_MAP_ALLOC 1
# include
# 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
(Ref.: prev. topic)