Recently a question has been asked to me in an interview.
The question was : Is the statement valid?
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.
Thanks in advance
30 Jan '13 - 17:57
Edited 30 Jan '13 - 18:21
Can the object delete itself? Yes it can.—SA
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.
This is one of the many articles explaining the reference counting technique and using
delete this. Don't be afraid of it, enjoy!
30 Jan '13 - 18:20
Edited 30 Jan '13 - 18:22
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.
30 Jan '13 - 19:00
It is perfectly normal
In fact, Microsoft even suggest using it for self-destroying Window objects here
] (search "delete this" on the page)
30 Jan '13 - 18:30
This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)