Game Development Community

Deleting Objects in Engine (C++)

by Nathan Bowhay - ESAL · in Torque 3D Professional · 02/17/2011 (2:08 pm) · 5 replies

I have sort of 2 questions both related. First:

Currently I have an associated SimObject for one of my classes. The dtor for my class currently looks like this:
MyClass::~MyClass()
{
	if(mMyPtr != NULL)
	{
		mMyPtr->unregisterObject();
		delete mMyPtr;
	}
}

Just in case it matters I have a method I opened to script to call when I want to create the associated object and it looks like this:
void MyClass::iniPtr()
{
	if(mMyPtr == NULL)
	{
		//Create a name for the object (Ptr_<ObjName>)
		String name("Ptr_");
		name += getName();

		//Create the object
		mMyPtr = new MyClass2();
		mMyPtr ->registerObject(name.c_str());
	}
}

Now that all appears to work (haven't had any issues thus far), but I need to implement a system to remove the MyClass object after a period of time if it's position hasn't been set after a certain period of time. Which brings me to the second part of the question. I set up a variable that holds the last time, in the MyClass ctor I set it to 0, then in set transform I have:
void MyClass::setTransform(const MatrixF& mat)
{
   Parent::setTransform(mat);

   //We got another position so update our time so we don't remove this object
   mLastPATime = Platform::getVirtualMilliseconds();
}
This is all standard timer stuff, but then in processTick I have:
void MyClass::processTick(const Move *move)
{
	Parent::processTick(move);

	//Check if this object should be removed
	if(mLastPATime != 0)
	{
		U32 timePassed = Platform::getVirtualMilliseconds() - mLastPATime;
		if(timePassed >= 2000)
		{
			//delete this in some way
			return;
		}
	}
}
Now I wasn't quite sure the correct way to have it delete it's itself. I found deleteObject(); and safeDeleteObject(); and then there is the unregisterObject(); I am assuming that deleteObject is the correct call (all safeDeleteObject appears to do is the same thing only schedules it to be a bit later), but I am not quite sure though what the correct way is though. So I figured i would post and ask, also it made me second guess what I was doing in the first part of the question when removing mMyPtr (although I should probably follow if it isn't broken don't fix it).

#1
02/17/2011 (3:57 pm)
The problem with self-deleting objects lies in thread safety. If the object deletes itself on one thread while it is being processed on an other thread then your program will crash. It can be done with the appropriate level of locks. Dr.Dobbs has an interessting article about this: article .

But it is generally simpler and safer to have a manager type class delete it for you. AKA the garbage collection approach.
#2
02/17/2011 (5:25 pm)
Well and I understand that issue. I understand it as a whole. My main question is about how torque handles it and what is the best method to call (T3D specific). It really is just a question for someone at GarageGames or who has substantially dealt with the garbage collection in torque.

I mean all 3 methods are part of the garbage collections just different parts and I am not entirely sure how they all work together or when what one should be used.
#3
02/23/2011 (5:49 am)
The right method for your processTick() is safeDeleteObject. The reason is that an object should not delete itself while being ticked or you will see crashes.

Generally, safeDeleteObject() is for when


  1. ... you want to delete a SimObject and are on a thread other than the main thread.
  2. ... you are in a script callback and want to delete a SimObject involved in the script code.
  3. ... you want to delete a SimObject currently being processed in a loop.

For the remainder, you can generally use deleteObject().

BTW, unregisterObject() only unregisters the object with the sim manager and does not delete the object. This is automatically triggered if you call either deleteObject() or safeDeleteObject().

Quote:garbage collection in torque

Just to be real anal here... Torque doesn't actually do garbage collection. SimObjects are not reference-counted and there is no other automatically memory scheme implemented for them. SimObjects are manually managed with most of the work being left to SimGroup.
#4
02/23/2011 (9:16 am)
Just to add to Rene's comments...
You'll often see (in script) a gMissionCleanup object that everything's added to - this is exactly why, the lack of garbage collection.
#5
02/23/2011 (11:52 am)
Good points, all makes great sense and I understood their wasn't real garbage collection just some code to help manage objects :) It is easy to use words that aren't quite accurate.

Thanks for all the great help guys, your awesome!