Jump to content

  • Log In with Google      Sign In   
  • Create Account

Awesome job so far everyone! Please give us your feedback on how our article efforts are going. We still need more finished articles for our May contest theme: Remake the Classics

#Actuallride

Posted 17 November 2012 - 10:14 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]

#1lride

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)    delete this;   //nothing will happen after this}[/source]

PARTNERS