Remove overload, recursive overflow

Hey guys, I wrote a quick test. I want delete to call deleteMe, which will then delete itself. The purpose of this is that I can remove the obj normally which libs stand out. (I don't want any glitches due to crt or w / e).

With the removal of this, I get stackoverflow, without it, msvc says that I skipped 4 bytes. When I don't call test i, the leak is 0. How can I remove the recursion problem? -edit to make it more clear. I want the LIB to call delete (thus deleteMe) instead of a program due to crt

class B { public: virtual void deleteMe()=0; static void operator delete (void* p) { ((B*)p)->deleteMe(); } }; class D : public B { public: void deleteMe() { delete this; } }; int test() { B *p = new D; delete p; return 0; } 
+1
source share
3 answers

The recursion is called by deleteMe calling delete , which calls B operator delete , which calls deleteMe again. It’s okay to overload operator delete (although you usually overload operator new ), especially when processing "alien" objects, which is most likely your case, but you should, in turn, call the current cleanup procedure from user delete .

In general, an overloaded operator delete should match operator new . In your case:

 B *p = new D; 

Here p is highlighted by the global new , so it must be freed using global delete . So:

 class D : public B { public: void deleteMe() { ::delete this; } }; 
+1
source

You should not override deletion. Especially this is not a function that ends with a delete call. Any deallocation of memory that you need to do (and you do not need to do in this example) should be done in the destructor.

+1
source

You are probably best off using the Destroy () method, which is expected to call your clients ... this will protect your lib from heap differences.

0
source

All Articles