views:

618

answers:

1

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?

+1  A: 

uint, int, Number and Boolean are not nullable in AS3. Number can be NaN, but that is really the best you can get. int and uint are 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 A with an event handler for a mouse movement on an object on some list, referencing an object 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).

back2dos
is there any way for me to debug the app and see what objects are being referenced to at that moment? this way I can see if it really is going to be garbage collected
Tom
@Tom - the Profiler (available in the Pro version of Flex) will give you that info. $500. Totally worth it, IMO, if you have the cash. The Profiler API is exposed though. Probably completely infeasible to build your own, but maybe you can get just the slice of functionality you need. http://livedocs.adobe.com/flex/3/html/help.html?content=profiler_3.html
Ross Henderson
@Tom - also, you might want to check out the seminal article on garbage collection in AS. There are definitely other articles out there, but this is the de facto 'one to read'. http://www.adobe.com/devnet/flashplayer/articles/garbage_collection.html
Ross Henderson