Skip to main content
gamedev.net
🔒 Locked

Overloading "delete"...

Started by Idov Feb 27, 2008 at 12:52 PM 8 replies 1.2k views
Original Post
Idov
Idov
Hi! I'm trying to overload the "delete" operator for debugging purposes. so I tried doing something like that:

void operator delete(void *p, char* file, int line)
{
	// Do something.
}
#define delete delete(__FILE__, __LINE__)
int main()
{
	int *g = new int;
	delete g;
	return 0;

}
but then i get this error: "delete : cannot delete objects that are not pointers" when I try to overload "delete" which receives only void* - it works fine... How can I use my delete with the 3 parameters? thanks :)
SiCrane
SiCrane
Basically, you can't. Non-standard overloads of operator delete are called iff the constructor throws in a placement new expression.
Idov
Idov
constructor of what?
I'm trying to overload the global delete.
ToohrVyk
ToohrVyk
Quote:
Original post by Idov
constructor of what?


The constructor called in the new-expression.

Quote:
I'm trying to overload the global delete.


You cannot overload the behavior of the delete keyword at the global level, and you certainly can't provide arguments to it.
Kippesoep
Kippesoep
If something goes wrong in a constructor and it throws an exception, the version of delete that corresponds to the version of new is being called. You cannot call the overloaded version directly.
Kippesoep
Idov
Idov
ok! thanks [smile]
Solias
Solias
Not overloading delete, but maybe something like this:

class DeleteTracker {    std::string file;    int line;public:    DeleteTracker(char* file, int line) : file(file), line(line) { }    template<class TYPE>    void operator=(TYPE* ptr) {        // Do something        delete ptr;    }    static DeleteTracker MakeDeleteTracker(char* file, int line) {        return DeleteTracker(file,line);    }};#define delete DeleteTracker::MakeDeleteTracker(__FILE__, __LINE__) = 


Seems like that would do what your code was intended to do.
Idov
Idov
Quote:
Original post by Solias
Not overloading delete, but maybe something like this:

class DeleteTracker {    std::string file;    int line;public:    DeleteTracker(char* file, int line) : file(file), line(line) { }    template<class TYPE>    void operator=(TYPE* ptr) {        // Do something        delete ptr;    }    static DeleteTracker MakeDeleteTracker(char* file, int line) {        return DeleteTracker(file,line);    }};#define delete DeleteTracker::MakeDeleteTracker(__FILE__, __LINE__) = 


Seems like that would do what your code was intended to do.


WOW! THANKS! :)
Idov
Idov
Hey! :)
I have another question...

I've overloaded "delete" with only one parameter (couldn't use Solias's code because I want to support also delete[]).
NOW I'd like to overload/override versions of the "new" operator which take a different nubmer of parameters.
In order to not change the debugged code, I use "#define new new(__FILE__, __LINE__)" but this will cause problems if I try to overload what I want.

Do you have any ideas how to do this?
thanks! :)

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.