It is not really failing in the preprocessor, it is a fairly well known limitation of the debug implementation. The preprocessor does textual substitution, so that the preprocessor does something like:
#ifdef _DEBUG
#define new _debugnew(__FILE__, __LINE__)
#endif
The preprocessor will then happily replace all instances of
new
with
_debugnew(__FILE__, __LINE__)
in your code. The implementation needs to provide an implementation of
_debugnew
, for instance:
void * operator new (size_t size, char const * file, int line)
{
logallocation(size, filem line);
return ::new char [size];
}
This works fairly well for the majority of uses of C++ memory allocation, i.e. for use of global new but does not work for all of them, as you have found.
As well as the way you got round this, you could possibly provide your own implementation of the debug new operator, that would get called in debug mode, e.g.
class MyClass
{
public:
void* operator new(size_t size)
{
return newImplementation(size);
}
void* operator _debugnew(size_t size, char const * file, int line)
{
return newImplementation(size);
}
private:
void* newImplementation(size_t size)
{
}
};
Note, that what is replaces
new
depends on your compiler - you will need to dig into the system files to find out what is actually used in debug mode.