Intermittent Bugs Related To Garbage Collection

I’ve been working with Actionscript 3.0 for some months now, and I’ve tripped over more than once when it came to garbage collection and memory management in general. Be very careful about letting local variables go out of scope, because the garbage collector may (or may not!) come to remove that variable before it finishes doing its job. This also results in one of the most frustration types of bugs - the intermittent bug.

For example, I used AS3’s TransitionManager class which allows you to create animations through code easily. I created an instance of the TransitionManager within an onclick handler, and used it to do slide a MovieClip onto the screen. However, sometimes the animation would stop before reaching the end. It might have stopped halfway, a quarter of way … every time it was different, but most of the time the slide in would complete. The problem was that the instance of TransitionManager was going out of scope as it was created in a method (in this case an onclick handler to be exact). Much of the time the garbage collector did not destroy the instance of the TransitionManager before it finished doing its animation, but sometimes it did, thus causing the animation to stop.

What I did was to make the instance of TransitionManager into a class variable. There might be other ways to do this though … Anyway the next time you see an intermittent bug in your program, it can be due to any reason - but remember this :-)

Leave a Reply


Easy AdSense by Unreal