Simulate download: Static vars not re-initialized

Tags:

The 'Simulate Download' feature in the Flash IDE is very useful. It can help debug race conditions that are a cause of intermittent problems. However, it can also lead you astray.

Problem: AS2 static variables are NOT reinitialized when you hit control-Enter the 2nd time to go into simulate-download mode.

If you'd like to try it, here's a simple test class:

class StaticVarInitTester {
	static public var n:Number = 100;
	static public var s:String = "hello";
	
	static public function main():Void
	{
		trace("n: " + n);
		trace("s: " + s);
		
		n++;
		s += s;
	}
}

Call StaticVarInitTester.main() function from frame 1. When you run the it, you will get:

n: 100
s: hello

When you then 'simulate download', you'll get:

n: 101
s: hellohello

If I had know this is how it behaved, I would not have waisted so much time debugging a non-existent problem in my swf. I should have realized this when I noticed that the output panel does not get cleared when starting simulate-download. It appears the program is just jumping back to frame 1 and running again without doing a full initialization.

From now on, I'll explicitly initialize static varibles in a static-init function if I plan on doing any simulate-downloads.

Comments

Sticky find, nice.