delete this;

Status
Not open for further replies.

levine

Ars Tribunus Militum
1,986
http://www.parashift.com/c++-faq-lite/f ... #faq-16.15

As long as you're careful, it's OK for an object to commit suicide (delete this).

Here's how I define "careful":
  • You must be absolutely 100% positive sure that this object was allocated via new (not by new[], nor by placement new, nor a local object on the stack, nor a global, nor a member of another object; but by plain ordinary new).
  • You must be absolutely 100% positive sure that your member function will be the last member function invoked on this object.
  • You must be absolutely 100% positive sure that the rest of your member function (after the delete this line) doesn't touch any piece of this object (including calling any other member functions or touching any data members).
  • You must be absolutely 100% positive sure that no one even touches the this pointer itself after the delete this line. In other words, you must not examine it, compare it with another pointer, compare it with NULL, print it, cast it, do anything with it.
If I saw it in code, I would be incredibly suspicious.
 

Bitruder

Ars Scholae Palatinae
885
I can't think of a situation where this would be the "best" solution. The only reason I can see for a suicide method would be if you want to perform some kind of last rites for the object. In that case though, I would just write a LastRights() method and then delete the object from outside the class after I call the LastRights() method. While there's no way to guarantee that it is done in this order, there's also no way to ensure you aren't murdering the object instead of calling the suicide method anyway. It's an extra line of code, but at least you don't have to worry about all of those "if you are 100% sure that..." cases. And we're in luck, because this is exactly what the destructor method does.
 

chaboud

Ars Centurion
313
Subscriptor
This is extremely common in reference-counting objects that are responsible for their own lifetime (e.g. COM), and I wouldn't be surprised in the least to run up against this in well-written C++ code.

I've been strongly advocating moving my group's code to self-lifetiming ref-counting objects because the alternative of contained, global, static, leaked, or singleton objects is just too much of a mess. Having an object know how to kill itself (or know how to tell a designated killer to kill it) is far better than leaving an ill-defined time-bomb in the code.
 

mChaboud

Ars Scholae Palatinae
674
levine":2v94yguk said:
Probably better to fix the mess (including making singletons forbidden) than change the paradigm.

My point is that C++ classes that manage their own lifetime is one really good way to fix the mess. If you either can't control or don't want to restrict the operating conditions of multiple objects, especially in a multi-threaded environment, a well-written ref-system is absolutely bang on what you want.

Wrap it up in smart refs and you have exceptions covered.

In short, I completely disagree with the assertion that one should be suspicious when seeing a "delete this;" in code, unless you mean suspicious that there is code considerate of object lifetime concerns.
 

levine

Ars Tribunus Militum
1,986
mChaboud":1g0m5hi8 said:
levine":1g0m5hi8 said:
Probably better to fix the mess (including making singletons forbidden) than change the paradigm.

My point is that C++ classes that manage their own lifetime is one really good way to fix the mess. If you either can't control or don't want to restrict the operating conditions of multiple objects, especially in a multi-threaded environment, a well-written ref-system is absolutely bang on what you want.

Wrap it up in smart refs and you have exceptions covered.

In short, I completely disagree with the assertion that one should be suspicious when seeing a "delete this;" in code, unless you mean suspicious that there is code considerate of object lifetime concerns.
You clearly have had a vastly different experience with C++ than I have. (shrug) Agree to disagree.
 

topham

Ars Legatus Legionis
13,756
Subscriptor
technophile":2vm7crqh said:
As with anything else, context matters. If you see a "delete this;" in an otherwise good-quality, reasonably well designed codebase, then it's probably not a red flag.

OTOH, if you found four submissions to thedailywtf.com in the first 100 lines of code, finding "delete this;" probably means you need another cup of coffee and some aspirin.

delete this;
this->foo();


Now that would be a wtf
 

wb

Ars Legatus Legionis
25,208
Subscriptor
My point is that C++ classes that manage their own lifetime is one really good way to fix the mess. If you either can't control or don't want to restrict the operating conditions of multiple objects, especially in a multi-threaded environment, a well-written ref-system is absolutely bang on what you want.


Until you get to mixed Managed/Native code and run into a native library that uses all smart pointers.

Now I have code in my managed C++ class like:

Code:
ref class MyManagedWrapper {
private:
   SmartPointer* smartypants;

public:
   MyManagedWrapper() {
      smartypants = &SmartPointerFactory::create();
   }

   void doSomething() { (*smartypants)->doSomething(); }
};

And of course, I *still* have to manually delete the smart pointer to boot.

Thanks for all the extra typing, dickheads!
 

wb

Ars Legatus Legionis
25,208
Subscriptor
You know I tried something like CAutoNativePtr and I cannot get it to compile. I end up getting incorrect syntax errors when it tries to compile my cpp file.

C2888 'identifier': symbol cannot be in namespace 'namespace'

Except that the declaration and definition are both defined in the same namespace, and both work fine so long as I don't try to use a template. The only thing that changed was I added the template class to the same namespace.
 
Status
Not open for further replies.