Archived

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

uncutno

Class selfdestruction

Recommended Posts

Is it posible for a class to delete itselfe from memory?
    
class A{public:
     char lots_of_data[10000];

     //like this

     void kill_me(void){
          delete this;
     };
     //or this

     void kille_me_with_pointer(A *pA){
          delete pA;
     };
};

main(){
    A *a;
    a = 0;
    a = new A;
    
    a->kill_me();
    //or

    a->kill_me_with_pointer(a);
    a = 0;
};
    
and if it is, is it wrong? Do you leak mem? [edited by - uncutno on April 30, 2002 12:27:51 PM]

Share this post


Link to post
Share on other sites
It works, but it''s a bad idea, because anyone referencing you may not know that you deleted yourself.

My general rule is that whoever creates, deletes. I don''t know of any way to have a class create itself, so in theory it shouldn''t delete itself.

Share this post


Link to post
Share on other sites
But i think i use it anyway...

I want a system where anyone can create anything, and the just throw it into a pool, witch handles itselfe... (also releasing)
a dynamic world :-) The problem with others trying to accses it, is no problem, since all pointers are double: a have a pointer to b, and b have a pointer to a

thanks!

Share this post


Link to post
Share on other sites
Yes, it can, but it''s something to be wary of. If you delete yourself, you must not call any members functions or access any member variables, you must guarantee that you were allocated with a new expression (and not a new[] expression), and that nothing will ever try to manipulate you after you''ve deleted yourself.

Share this post


Link to post
Share on other sites
You cannot be sure that the object has been created on the heap and not on the stack, and cannot know either if it is part of an array.


  
void foo( A* bar )
{
bar->kill_me();
}

void main()
{
A local;

A* v1 = new A;
A* v2 = &local;
A* v3 = new A[10];

foo( v1 ); // ok

foo( v2 ); // segmentation fault

foo( v3 ); // undefined behaviour

};
[source]

Otherwise, as pointed out, you have to make sure not to refer to <tt>*this</tt> after having <tt>delete</tt>d it.


[Questions (STFW) | GDNet Start Here | GDNet Search | Forum FAQ | Google | Asking Smart Questions ]
[Docs (RTFM) | MSDN | SGI''s STL | OpenGL | File formats]
[C++ Must Haves (RTFS) | MinGW | Boost | Loki | FLTK | SDL ]

Stolen from Magmai Kai Holmlor, who held it from Oluseyi, who was inspired by Kylotan...

Share this post


Link to post
Share on other sites
quote:
Original post by uncutno
I want a system where anyone can create anything, and the just throw it into a pool, witch handles itselfe...


You want the pool to do the deleting then, not the object deleting itself.

Share this post


Link to post
Share on other sites
quote:
You cannot be sure that the object has been created on the heap and not on the stack, and cannot know either if it is part of an array.

Depends. It could be a private inner class whose usage is restricted to things that you hae absolute control over.

Share this post


Link to post
Share on other sites
The technique of class deleting itself is used in COM. The one reason it is safe to do so in COM is because clients do not explicitly create the object via its constructor. Clients use a class factory interface to request that an object be created. This allows the component to create the object any way it desires. When the reference count of the object reaches 0, the object releases itself from memory (typically with delete this; in C++)

If you really want a managed "pool" of objects you should override new/delete in your class(es).

Dire Wolf
www.digitalfiends.com

Share this post


Link to post
Share on other sites
well.. an object should only delete itself when it can create itself...

and, can you be born from yourself? no.. needs sort of a mother..

now you say you cant be born from yourself, but you can die from yourself..

yes
but when you''re dead you''re not away.. just useless..

the collection of humans created you (actually, 2 where involfed)
when you''re dead, the collection of humans realise this at some point ("hy you? hy? HELLO? ARE YOU OKAY? WHATS UP WITH YOU?" "hm, he''s dead") and then THEY remove YOU.. not you..

what this means is:
if you create sort of a dynamic world holding your objects, then the world has to clean itself up, not the objects..

"take a look around" - limp bizkit
www.google.com

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
Please take a read of the C++ FAQ Lite (search for it). It contains many reasons why you shouldn''t do things like this. Apart from it being messy and error prone, you current implementation calls the destructor twice (undefined behaviour).

Share this post


Link to post
Share on other sites