Game Development Community

ITickable problems...

by Josh Moore · in Torque Game Engine · 03/04/2006 (4:25 am) · 5 replies

I have a class that uses the iTickable interface. It seems that sometimes when I delete an instance of this class, Torque crashes hard because iTickable is trying to tick a NULL object. This is a server side only object, so I'm not sure if it has anything todo with that. If anybody has any idea to what's happening, don't be shy to post!

Thanks, Josh.

#1
03/04/2006 (7:05 am)
It sounds as if the object isn't properly getting removed from the smProcessList when the deconstructor is called--or, the deconstructor isn't being called...

ITickable::~ITickable()
{
   for( ProcessListIterator i = smProcessList.begin(); i != smProcessList.end(); i++ )
   {
      if( (*i) == this )
      {
         smProcessList.erase( i );
         return;
      }
   }
}

I can't explain why my gut tells me this, but this particular code looks more appropriate in the ::onRemove() call of the base iTickable object instead of the deconstructor, or even exposed as a separate helper function so you could call it from wherever you like.

Also, a real quick fix might be to simply call ::setProcessTucks(false); on your object prior to deletion.
#2
03/04/2006 (7:50 am)
Does iTickable even have onRemove method? It's stand alone class, and IIRC, onRemove is a SimObject method.

I'm pretty much a beginner when it comes to C++, but could the fact that my class doesn't have a C'tor or a D'tor make a difference?
#3
03/04/2006 (9:12 am)
@Josh: good catch on the fact that iTickable is stand-alone. IT doesn't need (or want) an onRemove, but your class probably does (if the design makes it appropriate).

It is common that every object has both a construcor and a destructor, even if empty. I'm no huge c++ guru either, but I'm a bit surprised your resource works without one...maybe it's calling a parent somehow. It certainly appears to not be calling the iTickable's destructor, which does the work of cleaning up the object out of the smProcessList.

This is just a guess, but try creating a constructor and destructor for your class, copying the contents of iTickable's deconstuctor (leave the constructor empty for now) and see if this fixes your problem.
#4
03/04/2006 (6:02 pm)
Ok, I just realised that my class does have a constructor, but not a deconstructor(can't believe I missed that). I added blank deconstructor, but the problem is still there.

I put a breakpoint on this line while debugging:
smProcessList.erase( i );
And when my object was deleted, it was called, and therefore I assume it was removed from the tick list. So I hit continue, and the program crashed here:

if( tickCount )
      for( ; smLastTick != targetTick; smLastTick += smTickMs )
         for( ProcessListIterator i = smProcessList.begin(); i != smProcessList.end(); i++ )
            if( (*i)->isProcessingTicks() ) [b]<----[/b]
               (*i)->processTick();

So it's definetly not getting removed. Maybe this has everything todo with the object beign purely server side?
#5
03/06/2006 (11:13 am)
Greetings!

Josh, you may want to take a look at this thread:

www.garagegames.com/mg/forums/result.thread.php?qt=39389

One of the problems with the current ITickable implementation is deleting your object during one of the ITickable methods - which is what I suspect you're doing based on your description. If you happen to delete the last object in the tickable list, then you'll get a crash as the iterator ends up past the list. At the very least, even if you don't experience a crash, one of your ITickable objects will skip a tick.

Two things you could try:

1. Set up a different method of deleting your object (class) that doesn't take place during one of the ITickable methods.

2. Implement my ITickable replacement class ICSTickable in the thread above. The caveat is that I don't believe anyone else has tried it out yet, although it appears to work at my end. I guess it's time to turn it into a resource.

I hope that helps.

- LightWave Dave