Game Development Community

Deleting an Object During a Callback

by Peter Robinson · in Torque 2D Professional · 09/21/2015 (8:56 am) · 6 replies

The way safeDelete is supposed to work is that you call the function and the object is deleted later when it's safe to do so without causing any problems. In legacy T2D this meant you could call the safeDelete function from pretty much anywhere and things would be fine. In MIT though, the object will crash the game if it's in the middle of a callback when it tries to delete the object. Even if you call safeDelete directly from a callback function it will still work most of the time - and then crash unexpectedly.

This came up because I was moving an object using MoveTo. When it reached the destination I would delete it. I could just schedule to delete the object in 10ms or something, but there's so many callbacks. What if the object collides right when it is deleted or it has an onUpdate callback fire? Safe delete doesn't feel safe anymore.

So my question is, shouldn't safe delete just postpone the deletion of the object until the callback finishes, rather than crash the entire game?

If the answer is yes, then I'll try to make the change and do a pull request. Thanks for your input!

About the author

A programmer who has been using the Torque2D game engine since the early adopter days. My game is Pirate Code and it can be found at www.circuithive.com. It will be arriving on Steam in 2016.


#1
09/21/2015 (9:30 am)
That's the whole point to safeDelete(), so something is wrong and it isn't working right.
#2
09/21/2015 (9:57 am)
Exactly! So I take it you're in favor of fixing this?

After more time with the code it looks like all objects default to being ready to delete. Only ParticlePlayers ever set their state to not ready. Beside checking that one flag (which never changes on all objects except ParticlePlayers) the scene.processDeleteRequests function does nothing to ensure that an object is ready to delete.

The delete function itself then checks the callback flag and (by way of assert) crashes the engine. This seems entirely avoidable with just a little bit of code. I'm going to make the change.
#3
09/21/2015 (11:03 am)
I figured out my problem. No code change needed (although it's not a bad idea).

I was trying to avoid just this sort of problem, so I was deleting a script object using a schedule like this.

$s = new ScriptObject();
$s.schedule(10, "delete");

It worked fine in legacy T2D but now it crashes every time. Apparently the schedule is considered a callback and you can't delete an object from a callback. Problem solved.
#4
09/21/2015 (4:22 pm)
Might want to make that
$s.schedule(32, "delete");
Each tick is 32 milliseconds and it's possible that this will still occasionally crash. Or, I suppose it was possible - seen it happen, but it's been a long time.
#5
09/21/2015 (5:50 pm)
Ah so you were using a scheduled delete(), instead of a direct safeDelete() call, is what was causing the problem?
#6
09/22/2015 (2:48 pm)
Yes, but the object was a ScriptObject so there was no safeDelete (inherits from SimObject, not SceneObject).