Jump to content

  • Log In with Google      Sign In   
  • Create Account


An object needs to commit suicide


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
9 replies to this topic

#1 lride   Members   -  Reputation: 633

Like
1Likes
Like

Posted 17 November 2012 - 09:43 PM

I have the following code in my project

[source lang="cpp"]Entity::handleMessage(Message* msg){ if(msg->type==DIE) entityManager.destroy(this->ID);...}[/source]
I figured this is equivalent to

[source lang="cpp"]Entity::handleMessage(Message* msg){ if(msg->type==DIE) delete this;...}[/source]

Is this safe?
An invisible text.

Sponsor:

#2 Álvaro   Crossbones+   -  Reputation: 12918

Like
0Likes
Like

Posted 17 November 2012 - 10:02 PM

No, it isn't. Whoever owns this object will likely try to destroy it later on, and then you'll have a double deletion. The first version isn't safe either, because almost anything you do in the "..." section will be undefined behavior. The safe thing to do would be to schedule destruction of the object for the end of the frame, or something like that. entityManager would have a container of IDs to destroy and once per frame it would go through it and destroy everything in it.

#3 lride   Members   -  Reputation: 633

Like
0Likes
Like

Posted 17 November 2012 - 10:12 PM

No, it isn't. Whoever owns this object will likely try to destroy it later on, and then you'll have a double deletion. The first version isn't safe either, because almost anything you do in the "..." section will be undefined behavior. The safe thing to do would be to schedule destruction of the object for the end of the frame, or something like that. entityManager would have a container of IDs to destroy and once per frame it would go through it and destroy everything in it.


I am 100% sure that this object will get deleted only once. And will it be fine if I don't try to access the object after "delete this"?
[source lang="cpp"]Entity::handleMessage(Message* msg){...//do all kind of stuff beforehand if(msg->type==DIE) entityManager.destroy(this->ID);//(delete this;) //nothing will happen after this}[/source]

Edited by lride, 17 November 2012 - 10:14 PM.

An invisible text.

#4 Servant of the Lord   Crossbones+   -  Reputation: 18496

Like
4Likes
Like

Posted 17 November 2012 - 10:31 PM

It's "fine" if the object never gets accessed again, but it's bad practice.

Ask yourself, who manages the lifetime of that entity? All variables have someone managing their lifetime, whether they be dynamically allocated or on the stack. What class should logically manage the lifetime of an entity? The next question is, why isn't that class managing it?
It's perfectly fine to abbreviate my username to 'Servant' rather than copy+pasting it all the time.
All glory be to the Man at the right hand... On David's throne the King will reign, and the Government will rest upon His shoulders. All the earth will see the salvation of God.
Of Stranger Flames - [indie turn-based rpg set in a para-historical French colony] | Indie RPG development journal

[Fly with me on Twitter] [Google+] [My broken website]

[Need web hosting? I personally like A Small Orange]


#5 Álvaro   Crossbones+   -  Reputation: 12918

Like
1Likes
Like

Posted 17 November 2012 - 10:33 PM

You should read this from the C++ FAQ lite . If you still think that you are safe having the suicidal objects, go ahead.

Even so, I don't like to write classes that impose unreasonable constraints on the user (objects have to be allocated using new, or through an entityManager...). I like to be able to have local variables of this type, arrays of these things and what have you.

As an alternative solution, your objects could have a callback (or a signal, which is similar) that they will use when they want to die. Whoever creates the object will also provide the mechanism to get rid of it.

#6 amanjit   Members   -  Reputation: 103

Like
0Likes
Like

Posted 17 November 2012 - 10:34 PM

http://www.parashift.com/c++-faq-lite/delete-this.html

#7 alnite   Crossbones+   -  Reputation: 2069

Like
0Likes
Like

Posted 18 November 2012 - 04:20 AM

Wait, entityManager is the one that's doing the delete?

#8 lride   Members   -  Reputation: 633

Like
0Likes
Like

Posted 18 November 2012 - 08:19 AM

Wait, entityManager is the one that's doing the delete?

yes, but i found a better solutiom. Just keep a boolean variable "alive" and delete the dead object in the next frame
An invisible text.

#9 Álvaro   Crossbones+   -  Reputation: 12918

Like
5Likes
Like

Posted 18 November 2012 - 08:41 AM


Wait, entityManager is the one that's doing the delete?

yes, but i found a better solutiom. Just keep a boolean variable "alive" and delete the dead object in the next frame


What do you mean you "found"? I gave it to you. Posted Image

Edited by Álvaro, 18 November 2012 - 08:41 AM.


#10 Khatharr   Crossbones+   -  Reputation: 2961

Like
0Likes
Like

Posted 19 November 2012 - 06:25 PM


Wait, entityManager is the one that's doing the delete?

yes, but i found a better solutiom. Just keep a boolean variable "alive" and delete the dead object in the next frame


Garbage collection - yes. This is what people were suggesting.
void hurrrrrrrr() {__asm sub [ebp+4],5;}

There are ten kinds of people in this world: those who understand binary and those who don't.




Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS