I have a class which is called a number of times. When the application goes to the next stage these all have to be unloaded. Because of that, I created an unload() method in the class.
The problem is that I can’t seem to set my uint variable “charId” to null in order to “unset” it. The “delete” command is not possible either as that is only applicable for dynamic variables or something in that kind of way.
Now I wonder, how am I supposed to unset this variable, so it’s memory will be re-allocated later on?
The class’s unload method:
public function unload():void
{
trace("Unloading character with charname '" + charName + "'.");
enterButton.removeEventListener(MouseEvent.CLICK, enterClicked);
removeChild(enterButton);
enterButton = null;
charName = null;
charId = null; //this is possible but not recommended - what's a better way?
lobbyInterface = null;
}
So yeah, it’s practically possible as it changes the variable type – however it’s not recommended and raising a warning. So, what’s a better way to do it?
Note that this object is also unloaded in it’s parent. Does that also free all these variables from memory?
uint,int,NumberandBooleanare not nullable in AS3. Number can beNaN, but that is really the best you can get.intanduintare always just 32 bit, so you can’t stuff a null-reference in there.The type of cleanup you are trying to do cannot be accomplished since AS3 has the concept of sealed classes. A sealed class has a fixed size in memory. When it comes to instance variables, think of it as a C
struct, you can only dump all of it, or nothing. You can do anything in C of course, it’s a fixed block in memory, an entity of one reference per variable.What you want to do is only work with dynamic variables, which are maintained differently.
You don’t need to do this sort of cleanup since Flash has garbage collection like most runtimes nowadays. It also deals with nested and circular references, the only thing you have to be sure about is, that you delete any “outer” references to that class. Things that are generally not collected are objects on the display list, running timers and intervals, and I/O related stuff. As soon as you have a reference chain from there to your object, it will not be collected.
Let us say you have an
object Awith an event handler for a mouse movement on an object on some list, referencing anobject B. B will not be collected, but as soon as there is no chain leading to an object, it will be collected (sooner or later, the GC is quite lazy. But the more memory you use, the more it does its work).