Hey guys i wrote a quick test. I want delete to call deleteMe which will then delete itself. The purpose of this is so i can delete obj normally which are allocated by a lib. (i dont want any crashes due to crt or w/e).
With delete this i get a stackoverflow, without it msvc says i leaked 4 bytes. When i dont call test i leak 0. How do i delete w/o a recursion problem? -edit- to make this more clear. I want the LIB to call delete (thus deleteMe) instead of the 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; }
The recursion is due to
deleteMecallingdelete, which calls B’soperator deletethat callsdeleteMeagain. It’s OK to overloadoperator delete(although you usually overloadoperator newas well), especially when handling ‘foreign’ objects which is likely your case, however you must in turn call the actual cleanup routine from within the customdelete.In the general case an overloaded
operator deletemust match theoperator new. In your case:Here
pis allocated with the globalnew, so it must be freed with the globaldelete. So: