Class selfdestruction

Started by
12 comments, last by uncutno 21 years, 11 months ago
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]
-Anders-Oredsson-Norway-
Advertisement
''Source'' has a u.
Chess is played by three people. Two people play the game; the third provides moral support for the pawns. The object of the game is to kill your opponent by flinging captured pieces at his head. Since the only piece that can be killed is a pawn, the two armies agree to meet in a pawn-infested area (or even a pawn shop) and kill as many pawns as possible in the crossfire. If the game goes on for an hour, one player may legally attempt to gouge out the other player's eyes with his King.
Io comprende.. :-)

me no english word...
-Anders-Oredsson-Norway-
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.
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!
-Anders-Oredsson-Norway-
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.

char a[99999],*p=a;int main(int c,char**V){char*v=c>0?1[V]:(char*)V;if(c>=0)for(;*v&&93!=*v;){62==*v&&++p||60==*v&&--p||43==*v&&++*p||45==*v&&--*p||44==*v&&(*p=getchar())||46==*v&&putchar(*p)||91==*v&&(*p&&main(0,(char**)(--v+2))||(v=(char*)main(-1,(char**)++v)-1));++v;}else for(c=1;c;c+=(91==*v)-(93==*v),++v);return(int)v;}  /*** drpizza@battleaxe.net ***/
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...
"Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it." — Brian W. Kernighan
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.
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.
char a[99999],*p=a;int main(int c,char**V){char*v=c>0?1[V]:(char*)V;if(c>=0)for(;*v&&93!=*v;){62==*v&&++p||60==*v&&--p||43==*v&&++*p||45==*v&&--*p||44==*v&&(*p=getchar())||46==*v&&putchar(*p)||91==*v&&(*p&&main(0,(char**)(--v+2))||(v=(char*)main(-1,(char**)++v)-1));++v;}else for(c=1;c;c+=(91==*v)-(93==*v),++v);return(int)v;}  /*** drpizza@battleaxe.net ***/
or reference counted with a private dtor
- The trade-off between price and quality does not exist in Japan. Rather, the idea that high quality brings on cost reduction is widely accepted.-- Tajima & Matsubara

This topic is closed to new replies.

Advertisement