dan@dyndata.UUCP (Dan Everhart) (06/28/90)
Is it kosher for an object to delete itself? To wit:
class Obj
{
/* ... */
void Suicide () { /* ... */ delete this; }
// Assumption: "Suicide" only called for Obj's which were
// created with "new" and only when the pointer will never again
// be used.
/* ... */
}
steve@taumet.com (Stephen Clamage) (06/28/90)
In article <685@dyndata.UUCP> dan@dyndata.UUCP (Dan Everhart) writes: >Is it kosher for an object to delete itself? To wit: > >class Obj > { > /* ... */ > void Suicide () { /* ... */ delete this; } > /* ... */ > } Yes, but do be careful, as noted in your full example. I have done this in a class destructor, but would not do it in a normal member function. After the delete, there is no guarantee that the class data makes any sense, and in particular that the virtual table is intact! So a later destructor may try to operate with garbage. This is a particular worry with virtual destructors when the virtual table is trashed. -- Steve Clamage, TauMetric Corp, steve@taumet.com
vaughan@mcc.com (Paul Vaughan) (06/29/90)
From: steve@taumet.com (Stephen Clamage) In article <685@dyndata.UUCP> dan@dyndata.UUCP (Dan Everhart) writes: >Is it kosher for an object to delete itself? To wit: > >class Obj > { > /* ... */ > void Suicide () { /* ... */ delete this; } > /* ... */ > } Yes, but do be careful, as noted in your full example. I have done this in a class destructor, but would not do it in a normal member function. After the delete, there is no guarantee that the class data makes any sense, and in particular that the virtual table is intact! So a later destructor may try to operate with garbage. This is a particular worry with virtual destructors when the virtual table is trashed. I'm having trouble making sense out of this. One reason to have objects destruct themselves is for referencing counting, eg. void Foo::UnRef() { count -= 1; if(!count) delete this; } Of course it's important that the code (object, whatever) that thought it had a reference not use the pointer after calling UnRef. Also, it's important that you don't execute anything that needs object data in the UnRef function after you delete this. Other than these considerations, I'm not aware of any reason not to delete this from a regular member function. You would clearly never want to do this Foo::~Foo() { . . . delete this; } First, it's already being deleted, second it would cause double deallocation. I'm assuming that's not what you meant, but that's what it sounded like. Paul Vaughan, MCC CAD Program | ARPA: vaughan@mcc.com | Phone: [512] 338-3639 Box 200195, Austin, TX 78720 | UUCP: ...!cs.utexas.edu!milano!cadillac!vaughan
jimad@microsoft.UUCP (Jim ADCOCK) (06/30/90)
In article <685@dyndata.UUCP> dan@dyndata.UUCP (Dan Everhart) writes: >Is it kosher for an object to delete itself? I believe so, but I've never seen anything definitive actually specifying its permissibility. Its always worked for me. Also, destructors traditionally are implemented [implicitly] so as to do the freeing of the space that their objects take -- if the destructor is called via delete. So destructors could be taken as an example where objects delete themselves. But, destructors are special functions -- which makes them a less than totally definitive answer to this question.
steve@taumet.com (Stephen Clamage) (07/01/90)
In article <9299@cadillac.CAD.MCC.COM> vaughan@mcc.com (Paul Vaughan) writes: > From: steve@taumet.com (Stephen Clamage) > In article <685@dyndata.UUCP> dan@dyndata.UUCP (Dan Everhart) writes: > >Is it kosher for an object to delete itself? To wit: > >class Obj > > { > > void Suicide () { /* ... */ delete this; } > > } > Yes, but do be careful, as noted in your full example. I have done this > in a class destructor, but would not do it in a normal member function. > [... ] >I'm having trouble making sense out of this. [...] If you have a color monitor, insert my red face here. Michael Tiemann was also kind enough to point out that what I said could not possibly have been what I meant. Please disregard my previous posting, and in future I'll try not to respond to net news before my morning coffee. You can "delete this;" in a member function, but as in my earlier posting, you must be very careful to avoid a second deletion by a call to the destructor, and also avoid any later reference to any virtual functions (including virtual destructors). In other words, it's dangerous. -- Steve Clamage, TauMetric Corp, steve@taumet.com
mckenney@sparkyfs.istc.sri.com (Paul Mckenney) (07/05/90)
In article <685@dyndata.UUCP> dan@dyndata.UUCP (Dan Everhart) writes: >Is it kosher for an object to delete itself? To wit: >class Obj > { > void Suicide () { /* ... */ delete this; } > } The following example appears on page 66 of Ellis and Stroustrup: void X::my_delete(Arena* a) { this->X::~X(); // explicit call of destructor if (a) a->free((void*)this); else delete (void*)this; } The text preceding the example says that my_delete is a virtual function (the class body for X is not shown). I presume that if there was no Arena parameter, it would be safe to allow the delete operator invoke the destructor (it has always worked for me... :-). Thanx, Paul