mardi 27 septembre 2016

How to overload operator new and delete to track memory?

I would like to track memory to check leaks and checking memory consumation (peek and so on) by overloading new/delete.

However, I have noticed that sometimes one delete is called while there is no corresponding new (even on simple programs with few lines of code using boost), it looks like it is possible to obtain memory from another way (malloc ?) and that delete is happy to free it after. This leads me to problems because I need to store information (sizes) with allocation.

I have overloaded the following methods is there one function missing ? or is this a bug with my c++ compiler/boost version ?

void * operator new( size_t size );
void * operator new( size_t size, const std::nothrow_t& ) noexcept;
void * operator new[]( size_t size );
void * operator new[]( size_t size, const std::nothrow_t& ) noexcept;

void operator delete( void* ptr);
void operator delete[]( void* ptr);
void operator delete( void* ptr, const std::nothrow_t&) noexcept;
void operator delete[]( void* ptr, const std::nothrow_t&) noexcept;

I am running under windows and mingw-w64 gcc so I prefer to use a "code" solution rather than a specific tool solution ("valgrind is not available), also this will allow me to run the program almost normally.

Aucun commentaire:

Enregistrer un commentaire