Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C++
Hi All,
 
Recently a question has been asked to me in an interview.
The question was : Is the statement valid?
 
delete this;
 
Can someone please let me know, if this is valid or not (with reason)?
 
As per me, this is a kind of suicide and we should not use such statements. But as per interviewer in multithreaded application it is possible.But i am not sure how.
 
Please answer.
 
Thanks in advance
Posted 30-Jan-13 17:57pm
Edited 30-Jan-13 18:21pm
v2
Comments
Sergey Alexandrovich Kryukov at 31-Jan-13 0:23am
   
Why there is a doubt? :-) —SA
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

Can the object delete itself? Yes it can.
 
Moreover, do you know the technique of reference counting? Among many other things, it is used in COM. With reference counting, this is a standard technique of deleting objects and reclaiming heap memory: every user of object should call its method incrementing a reference counter, and, after use, call the method decrementing it. The last of these two methods is supposed to delete the object in the way you are asking about.
 
[EDIT]
 
This is one of the many articles explaining the reference counting technique and using delete this. Don't be afraid of it, enjoy!
 
Cheers,
—SA
  Permalink  
v2
Comments
sourabhmehta at 31-Jan-13 0:25am
   
Thanks for quick response. It will be highly appreciable. If you can provide me some simple example or some reference link for the same.
Sergey Alexandrovich Kryukov at 31-Jan-13 0:31am
   
I just did. The article I referenced is very simple. But... class A { public: void Delete() { delete this; } }; //... A* a = new A(); a->Delete(); However, this trivial sample is correct but pretty meaningless. Again, read the article. Locate "delete this" in code, and you will really see the value of it. And please accept the answer formally (green button) — thanks. —SA
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 3

Yes, delete this; has defined results, as long as you assure the object was allocated dynamically, and (of course) never attempt to use the object after it's destroyed.
 
Some consider this a nasty hack, and tell anybody who will listen that it should be avoided. One commonly cited problem is the difficulty of ensuring that objects of the class are only ever allocated dynamically. Others consider it a perfectly reasonable idiom, and use it all the time. Personally, I'm somewhere in the middle: I rarely use it, but don't hesitate to do so when it seems to be the right tool for the job.
 
Edit: [mostly in response to @Alexandre C's comment]: the primary time you do this is with an object that has a life that's almost entirely its own. One example James Kanze has cited was a billing/tracking system he worked on for a phone company. Basically, when you pick up the phone, something takes note of that and creates a phone_call object. From that point onward, the phone_call object handles the details of the phone call (making a connection when you dial, adding an entry to the database to say when the call started, possibly connect more people if you do a conference call, etc.) When you hang up, the phone_call object does its final book-keeping (e.g., adds an entry to the database to say when you hung up, so they can compute how long your call was) and then destroys itself. The lifetime of the phone_call object is based on when you pick up/hang up the phone -- from the viewpoint of the rest of the system, it's basically entirely arbitrary, so you can't tie it to any lexical scope in the code, or anything on that order.
 
For anybody who might care about how dependable this kind of coding can be: if you make a phone call to, from, or through almost any part of Europe, there's a pretty good chance that it's being handled (at least in part) by code that does exactly this.
  Permalink  
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

It is perfectly normal[^].
 
In fact, Microsoft even suggest using it for self-destroying Window objects here[^] (search "delete this" on the page)
  Permalink  

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Your Filters
Interested
Ignored
     
0 Sergey Alexandrovich Kryukov 9,225
1 OriginalGriff 5,395
2 Peter Leow 4,100
3 Maciej Los 3,540
4 Abhinav S 3,333


Advertise | Privacy | Mobile
Web02 | 2.8.140415.2 | Last Updated 31 Jan 2013
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Use
Layout: fixed | fluid