I'm making a memory leak detector for c++. It replaces the global new operator and uses a macro to initialise two global variables, __file__
and __line__
, like so:
#define new (__file__=__FILE__,__line__=__LINE__) && 0 ? NULL : new
I learnt this trick from another StackOverflow user whose name I can't remember. This works fine with simple operations involving new, however this appraoch causes problem when the user defines a local operator new for a namespace. For one thing, lines like
void* operator new(size_t size);
is also matched by the macro; also, explicit calls to global new, like:
int* i = ::new int;
causes syntactic errors.
Is there a way to redefine or suppress __LINE__
and__FILE__
constants (so that they show the file name and line number of the call to operator new) in part of the code? If not, how could the macro be improved to not match user defined "operator new" and not cause problem with "::new"?
I would really love to get rid of the wonkiness of macros. Thanks in advance :)