Skip to main content
gamedev.net
🔒 Locked

operator delete

Started by steli 4 replies 350+ views
Original Post
steli
steli
Hi! I have oveloaded the global delete operator in this way: void operator delete(void *pointer, void *__pointer); off corse i had to overload new operator. How can I call my delete operator when i''m deleting a pointer. If i write<> off course delete(void*) function is called, if i write ::operator delete(pointer, pointer) my delete function is called but object destructor isn''t called. How can i avoid this problem? Thinking at this i wonder why how is good overloading of delete operator, if you can''t called nicely. Thanks!
UltimaX
UltimaX
I think it''s something like:


inline void operator delete(void *MemoryAddress)
{
//--Process Function

//--free(MemoryAddress);

}
//--

inline void operator delete[](void *MemoryAddress)
{
//--Process Function

//--free(MemoryAddress);

}
//--

//--Override the delete with yours

//--

#define delete delete
//--



You could also make it like:

inline void operator delete(void *MemoryAddress, const char *File, int LineCalledFrom)
{
//--Process Function

//--free(MemoryAddress);

}
//--

inline void operator delete[](void *MemoryAddress, const char *File, int LineCalledFrom)
{
//--Process Function

//--free(MemoryAddress);

}
//--

//--Override the delete with yours

//--

#define delete delete(__FILE__, __LINE__)
//--



New is the same way:

inline void* operator new(unsigned int Size)
{
//--Process Function

//--return malloc(Size);

}
//--

inline void* operator new[](unsigned int Size)
{
//--Process Function

//--return malloc(Size);

}
//--

//--Override the new with yours

//--

#define new new


And of course you can use the __LINE__ and __FILE__ if your keeping track of all the memory.
See if this works how you want it. Add the #define delete delete



-UltimaX-

"You wished for a white christmas... Now go shovel your wishes!"
JohnBolton
JohnBolton
You can''t. It is not in the language. However, depending on what you are trying to accomplish, there are likely to be acceptable alternatives.
John BoltonLocomotive Games (THQ)Current Project: Destroy All Humans (Wii). IN STORES NOW!
Namethatnobodyelsetook
Namethatnobodyelsetook
The alternate version of operator delete with the extra parameter is ONLY called when the object constructor throws an exception. When deleting, if you have a standard operator delete as part of your class it will be used. If not, the regular global delete is called.

How is overloading delete useful? Well, I find it useful. Below is an overview of my memory system.

I have three versions of new. The normal one, one that takes an address (placement new), and one that takes file and line parameters.

The placement one just returns the pointer. Never call delete on an object that has placement new, since the new didn''t actually allocate space. You can write ~class(); to call the destructor though.

The standard new and the other new both allocate space of the object, plus a bit extra to store next, prev, file, line, size, and a debug index. Standard new sets file to 0, line to 0. The other new sets the file and line data appropriately. Both then set a debug index, set the size, add this to a list of allocations.

The debug index is just an int that is incremented when you ask for a new debug token. Later you can then search the linked list for any memory with a token >= your token. If there are any allocations, you have a leak in the enclosed code.

Anyway, back on topic. The delete will remove the object from the list, and free the memory. The same operations are used regardless of which new was used. I have 3 useful overloaded news, and one useful overloaded delete.

I also record the type of allocation (new, new[], or C style (I take over malloc, calloc, realloc, and free too). Delete, delete[], and free then make sure you''re freeing the appropriate type of allocation via an assert.
Trienco
Trienco
quote:
Original post by UltimaX

//--
//--Override the delete with yours
//--
#define delete delete
//--



now either this is a very bad joke or my understanding of the precompiler is extremely flawed. how would replacing every "delete" in the source with "delete" make even the slightest difference?

f@dzhttp://festini.device-zero.de
UltimaX
UltimaX
Namethatnobodyelsetook:
Sounds like you have a nice memory manager.

Trienco:
It was not a joke. Try the example I gave. Plug it into your code then add a message box inside of the ''delete'' function. You think a message box will come up?

Of course you can''t record where the ''delete'' macros are being called from but it will work. Why? It is Not possible to pass extra parameters to the overloaded delete macro. It would cause syntax error. If you wanted to record where the delete macros are being called from you have to make a sperate function for that.
Say:
#define delete (DeleteFunction(__FILE__, __LINE__), false) ? DeleteFunction("", 0) : delete

If you don''t want to record anything you can just use:
#define delete delete
and it will overload the ''delete'' with your function.

Dang, I love the Game Programming Gems series I learn more from them than anything.

Have a nice day.

-UltimaX-

"You wished for a white christmas... Now go shovel your wishes!"

Topic Locked

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

Sign in to reply to this topic.