An object needs to commit suicide

Started by
8 comments, last by Khatharr 11 years, 5 months ago
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.
Advertisement
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.

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]
An invisible text.
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?
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.
http://www.parashift.com/c++-faq-lite/delete-this.html
Wait, [font=courier new,courier,monospace]entityManager[/font] is the one that's doing the [font=courier new,courier,monospace]delete[/font]?

Wait, [font=courier new,courier,monospace]entityManager[/font] is the one that's doing the [font=courier new,courier,monospace]delete[/font]?

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.

[quote name='alnite' timestamp='1353234015' post='5002003']
Wait, [font=courier new,courier,monospace]entityManager[/font] is the one that's doing the [font=courier new,courier,monospace]delete[/font]?

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

What do you mean you "found"? I gave it to you. smile.png

[quote name='alnite' timestamp='1353234015' post='5002003']
Wait, [font=courier new,courier,monospace]entityManager[/font] is the one that's doing the [font=courier new,courier,monospace]delete[/font]?

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

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.

This topic is closed to new replies.

Advertisement