Game Development Community

1.7.2 - Crashes with t2dSceneGraph::removeFromScene

by Vern Jensen · in Torque Game Builder · 06/03/2008 (4:04 pm) · 5 replies

It appears to be undocumented that t2dSceneGraph::removeFromScene() willl crash the whole program if called from within certain callbacks. So far I've discovered, completely by accident, two callbacks that cause this crash:

t2dAnimatedSprite::onAnimationEnd()
t2dSceneObject::onWorldLimit()

There are likely other callbacks that are dangerous for this method as well.

I've discovered that if I schedule the function call instead, everything works just fine. But documenting this, or even finding a way to avoid the crash, would be better. TOok me a while to figure out why the program was crashing... undocumented "dangerous" functions that you shouldn't call in certain situations should be avoided IMO.

#1
06/03/2008 (9:31 pm)
Vern

I completely agree that there should be more documentation regarding functions such as ::removeFromScene(), and the application should probably be a bit more graceful when encountering such conditions.

However, as I read over this post, I had to think... and it may be that I'm misunderstanding, but I'm figuring that attempting to remove an object from within a callback on said same object is probably a bad idea...

you're most likely attempting to remove this object while there's still a reference outstanding due to the fact you're in a callback that's going to return execution back to the calling object...

i.e. deallocation of an object from within itself, Most languages will allow you to shoot yourself in the foot in this same way unfortunately. Which is probably why the scheduling of the remove works, because the object has time to complete the callback and remove any internal references before the scheduled function runs.

Just my observation.

Take care.
#2
06/04/2008 (10:23 am)
I'm sorry but I don't understand, are you doing C++ work here?

Not to sound too harsh but this is not a bug, you can break stuff in a million ways by working in C++ and no amount of documentation will save you. This is a common issue and not easy to fail gracefully from in all cases. What you've done is pretty much the most serious thing you can do to an object.

To counter this, you should use the "safeDelete()" method which is well documented including the reason why you should use it which is, in all cases of a callback, where (as Scott mentions) there's further processing that could happen.

In all callback cases you should assume that you're not at liberty to do an immediate deletion or remove it from the scene. Again, the "safeDelete()" allows you do to this safely. There isn't however a safe way to remove the object from a scene from within a callback related to the scene or an object within that scene.

C++ documentation? You're kidding right? ;)

EDIT: Sorry, I realised that you are talking about the script callbacks not C++. Same thing still applies in that you should use ".safeDelete()" on all T2D objects, especially within callbacks. You can find the reference here but it's also used in pretty much every tutorial on TDN.

Hope this helps,

Melv.
#3
06/04/2008 (12:24 pm)
No no, this from script, and yes, I do use safeDelete() when deleting any object, but recently I discovered that creation/deletion of objects was really slowing down my game, so I created a SimSet() to store objects not in use instead of creating/deleting them, and therefore all my safeDelete() calls got converted to my own function that removes the object from the scene using removeFromScene().

I wondered at first why the program was crashing. Intuition did, of course, tell me, as Scott pointed out, that maybe I shouldn't call removeFromScene() from within a callback, but this actually isn't quite as obvious as Scott makes it sound. It still took a little while for me to figure it out and put it in a schedule() call instead. I'm just trying to save other people from the same frustration of trying to figure out why their program is crashing, even if it is something they "should" just know.
#4
06/04/2008 (12:29 pm)
BTW, thanks for posting the link to the docs, Melv. I've just added a brief note to that function. I tried doing that before even starting this thread, but couldn't find a working version of the documentation online. Odd, it seems to be working now though.
#5
06/05/2008 (11:12 am)
Sorry I got a little confused there. It's my fault for speed-reading the forums as I like to answer as many as I can in the little time I have.

It's working, that's what's important. :)

Melv.