Click here to Skip to main content
15,886,110 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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
Updated 30-Jan-13 18:21pm
v2
Comments
Sergey Alexandrovich Kryukov 31-Jan-13 0:23am    
Why there is a doubt? :-)
—SA

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
 
Share this answer
 
v2
Comments
sourabhmehta 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 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
It is perfectly normal[^].

In fact, Microsoft even suggest using it for self-destroying Window objects here[^] (search "delete this" on the page)
 
Share this answer
 
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.
 
Share this answer
 

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



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900