Test Movie and AS2 Garbage Collection Hilarity
Want to scramble the Flash 8 compiler’s brain? (Or more likely your own?) Give the following class a quick whirl.
class TestArray {
private var foo:Array = new Array();
public function TestArray() {
this.foo.push("bar");
trace("this.foo.length ––> " + this.foo.length);
}
}
I know, I know. Bad form. ‘Tis true. The class property foo isn’t really a compile-time constant. But the compiler says nary a word in protest. No errors thrown. Nada. Zip. Zero. Nothing. Which would be fine if the Flash IDE didn’t get all butterfingered on you.
But try creating a new instance of TestArray in your Flash movie and invoke “Test Movie” a few times without closing the window. The length of foo will just grow and grow and grow. It’s only cleaned up between tests if you close out the Test Movie window and start over again.
I could be wrong, but methinks I smell a compiler garbage collection issue (albeit one surrounding bad code). Shouldn’t the compiler be smart enough to let you know when you’ve done something that’s basically foolish?

2 Comments
Jeff
The ‘this’ keyword is simply a confusing concept in AS2. In this scenario, this is likely a reference which the flash compiler treats as static, which is incorrect. However, the simply fix would be simply to omit this and do foo.push(“bar”); Now the garbage collector should work as intended, though I haven’t actually tested it…
Jeff
Okay, after testing a bit, the ‘this’ keyword seems to make no difference. It seems like the compiler treats objects that are instantiated at the class level as statics. The only way to avoid this issue it seems is to new an object inside the constructor instead of at the class level.
class TestArray
{
private var foo:Array;
public function TestArray()
{
foo = new Array();
foo.push(“bar”);
trace(“this.foo.length ––> ” + this.foo.length);
}
}
That works as expected. Flash can be dumb.