Archived

This topic is now archived and is closed to further replies.

CommanderXXL

delete this; from within class member legal?

Recommended Posts

Is a delete this; call from within some member function of a class legal? Lets say i write a Linked List class and have a function: void LL::Delete(int Index) { if(Index == MyIndex) delete this; } or does the class crash when it is done executing it''s destructor because it wants to jump back in the delete function and it''s not present anymore. By CommanderXXL

Share this post


Link to post
Share on other sites
I believe that it''s not defined in the C++ standards, but in most implementations (VC++ and gcc that I know of, probably others), it''s perfectly OK, as long as you:

  • Are absolutely sure that it''s OK to call delete on the object at all. It''s no good doing something like:
      
    LL obj;
    obj.Delete();

    Since you couldn''t call delete on it anyway. A good idea is to have a static member which constructs a new object, and then use Delete() to destroy it, as in:
      
    LL *pObj = LL::Create( ... );
    pObj->Delete();

  • You never try to access the pObj after you''ve called it''s Delete() method (this should be obvious), and
  • You don''t use any non-static member variables inside the Delete() method, after you''ve done the delete this. The best idea is to have "delete this" as the last statement in the Delete() method.


Good luck!


codeka.com - Just click it.

Share this post


Link to post
Share on other sites
It is defined as working in C++.
In fact if destructors can be virtual it''s (partly) because of this.

If you derivate a class from a super class, you need to define the super class destructor as virtual to get the object cleanly deleted from memory.

Anybody having made an automatic memory management class COM like know how usefull it is to ''delete this;''

just don''t forget about the virtual parent class destructors.

-* So many things to do, so little time to spend. *-

Share this post


Link to post
Share on other sites
As was mentioned before, it''s no good to call delete on an automatic variable (one you didn''t create with new). If you want to make sure that an automatic variable can never be created, one option is to make the destructor private. That way only your "Delete()" member, the function that calls delete this, will have access to the destructor and the compiler won''t even let you compile any code with automatic variables using the class in question. If you ever want to derive a class from this class you should make the destructor protected instead of private, and declare it virtual, as mentioned before.

This method is a little simpler than using the static class idea, and I think it''s a lot cleaner though perhaps slightly less intuitive.

Hope this helps -
- Riley

Share this post


Link to post
Share on other sites
One other point to remember is after calling delete all varables of this are assumed to be invalid, so if your method returns something you''ll have to store it off first, ie.

  

int SomeClass::Release(void)
{
int Cnt= --this->RefCount;

if( this->RefCount < 1 )
delete this;

return(Cnt); // returning this->RefCount may be invalid

}



Share this post


Link to post
Share on other sites