I'm creating a debug class called debug_mem_allocation for the purpose of finding memory leaks. I used macro's to replace the new and delete operators. My problem is with trying to replace the delete[] operator with a macro. I can't replace the delete[] operator by using void* as the first parameter, because then my code will not be able to modify the calling function's pointer, nor would it get the source file name and line number. My current work around has been to modify the source code by replacing delete[] with DELETE_ARRAY_AND_NULLIFY, which is defined as delete[] for NON-DEBUG compile, and defined as an operator-() of my debug class for DEBUG compile. Can anyone think of a better way to replace the delete[] operator via macro that would not require me to modify the source code? My compilers are VC++ 6.0 and GNU 3.x Here's the header:
That's why there's an overloaded version of operator+() template<typename T> void operator+(const T &ptr) { Log(ptr, DeAllocMem); delete ptr; } The overloaded version with the const does not try to set the pointer to a NULL, but it does do the logging. If delete is given an rvalue, this operator is used instead of the following: template<typename T> void operator+(T &ptr) { Log(ptr, DeAllocMem); delete ptr; ptr = NULL; } The only problems I've seen with this is with VC++ in which I still get compile errors for something like the following: delete this; Which gives me the following error: error C2667: '-' : none of 2 overload have a best conversion error C2593: 'operator -' is ambiguous However, I don't get this error with the GNU compiler, so I'm thinking this is a bug in VC++. In any case it's rare to see this type of code, and it can easily be modified to the following: foofoo* t = this; delete t; Which makes VC++ happy. fraught with perils suggest to me many problems. Is there something else you see wrong with the code? And I still think there's a C++ Guru out there who can think of a better work-around for delete[] operator then what I have.