Jump to content
  • Advertisement

Archived

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

uncutno

Class selfdestruction

This topic is 5895 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

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
Advertisement
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

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!