Game Development Community

ScriptObject and Garbage Collection

by Peter Robinson · in Torque Game Engine · 07/10/2012 (2:09 pm) · 5 replies

This question has been touched on several times in the past, but looking through the forums I never really found a straight answer.

If I create a ScriptObject, will it be deleted once I loose the reference or do I need to delete it myself? I ask because I realized today that most of my ScriptObjects (and I have a lot) are never deleted. Some of them have schedule loops which could really be slowing things down over time - unless there's a garbage collector.

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
07/11/2012 (8:07 am)
TorqueScript doesn't have any sort of automatic garbage collection and its enforcement of scoping rules is a bit unusual:

Local variables (i.e. %myVar) defined within a function exist only within the function scope and are 'freed' when the function exits.

Global variables (i.e. $myVar) exist in a global scope, even when defined from within functions. Global variables are also never freed unless you call DeleteVariables().

"Objects" such as ScriptObject, SimSet/SimGroup, ArrayObject, etc. also exist globally and are never freed until their Delete() method is called. The catch here is that you can define one of these objects without giving it a name or assigning a variable to it (i.e. new ScriptObject(); instead of %myObject = new ScriptObject(); or new ScriptObject(MyObject);). This is essentially a memory leak as you'll have an unreferenced object that will exist for the lifetime of the program.

TL;DR version: call .delete() on any objects you create via 'new'.
#2
07/11/2012 (12:20 pm)
Thanks Chris! That's exactly what I needed to know. Consequently it means that my game is riddled with leaks, but at least now I know to fix them. Thanks!
#3
07/13/2012 (12:37 pm)
I'm off your subject here, but one thing I finally picked up on: when you delete a SimObject, references to it will be removed automatically from any SimSets. Kinda cool.

%obj = new ScriptObject();

%set = new SimSet();
%set.add(%obj);

%obj.delete();

echo(%set.getObjectCount());  // will print "0"

This can get confusing if you are looping through a SimSet and deleting objects. :)
#4
07/14/2012 (11:30 pm)
Yep. That's one of those "gotchas!" with SimSets/SimGroups. There are a few more worth noting:

1). Objects can exist in multiple SimSets at the same time but only one SimGroup. Adding an object to one SimGroup and then attempting to add it to another will simply move it from the first SimGroup into the second SimGroup.

2). SimSets don't "own" the objects they contain whereas SimGroups do. When deleted, SimSets won't delete their children but SimGroups will.

3). Objects can exist in both a SimSet and a SimGroup simultaneously.

#2 and #3 are especially important. Consider a case where you have an object in a SimSet and also a SimGroup but the SimGroup is deleted first. The object it contained will also be deleted and now the SimSet is empty. That can be totally unexpected behavior if you're not aware of the differences between the two container classes.
#5
07/17/2012 (6:48 am)
Good information guys. Thanks!