Flash: Beware the Garbage Collector August 3, 2008
Of all the major changes in AS3, this has been the most problematic....things hanging out off the display list, unreachable. I discovered a new way to get these "voices from the grave" to happen this eve.
We are instantiating new MovieClip classes for the stage via
-
-
MovieClipClassReference = getDefinitionByName(styleMC) as Class;
-
try{
-
curPageClip = new MovieClipClassReference (curPageText);
-
}catch (e:Error){
-
curPageClip = new MovieClipClassReference ();
-
}
The idea being sometimes they accept an argument, in other times they don't. If it doesn't work the first time, then we try the second. Unfortunately this means that there are now 2 MovieClips, with all their assets hanging out in memory, but lacking the brains/script because the constructor errored out! In our case the error was 'voices from beyond' as the sounds embedded on the timeline were playing in the background even though there only appeared to be one instance on the stage stopped at the beginning.
The short solution for us was to have all classes accept a constructor, even if they don't do anything with it. A cleaner approach is to introspect the class looking for the amount of arguments in the constructor...
-
-
var desc:XML = describeType(MovieClipClassReference );
-
var xmlL : XMLList = desc..constructor.parameter;
-
trace("Constructor Parameters " + xmlL.length());
-
if( xmlL.length() == 1) {
-
curPageClip = new MovieClipClassReference (curPageText);
-
}else if( xmlL.length() == 0) {
-
curPageClip = new MovieClipClassReference ();
-
}

Add a Comment: