I want to replace new with a version which (in debug mode) tracks allocations, can tell me the memory usage, the number of allocations, report memory leaks etc. My initial thoughts was doing something like Fluid Studio’s MMGR and define a macro called new which pointed to my custom version of new. Like this:
inline void* new(size_t size,unsigned int line,const std::string& file)
{
//…
}
#define new new(__LINE__,__FILE__)
There is one very big problem with this though, imagine this class:
class a
{
public:
static void* operator new(size_t size)
{
return ::operator new(size);
}
};
The problem is the preprocessor will replace new with something like new(5,”a.hpp”). So the code the compiler would get would look like this:
class a
{
public:
static void* operator new(4,"a.hpp") (size_t size)
{
return ::operator new(6,"a.hpp")(size);
}
};
And this will give me a syntax error, so is there any work around for this? The only solution I can see is defining a macro custom_new to new(__LINE__,__FILE__) instead of new to new(__LINE__,__FILE__).