I have a Flex app that loads and unloads numerous SWFs throughout the use of the app, and I have one SWF that doesn’t seem to want to unload. I’ve done all the tricks, used unloadAndStop(), made sure all my listeners used weak references, etc. Here is the code of the external SWF, it’s a simple snowflake generator. Would anybody be able to glance through and determine what is preventing it from being garbage collected?
Also, I have determined that if I try to unload this SWF before it has ever been added to a display list, it will unload successfully. If I add it to a display list and then remove it, it won’t get unloaded.
Also, I ended up adding the destroy() function, just to see if I called it before unloading the SWF if it would help. It doesn’t.
Many thanks
import flash.filters.BlurFilter;
closeBtn.addEventListener(MouseEvent.CLICK, handleCloseClick, false, 0, true);
function handleCloseClick(e:MouseEvent):void
{
dispatchEvent(new Event("PROCEED", true, false));
}
var snowflakes:Array = [];
var snowTimer:Timer = new Timer(100);
snowTimer.addEventListener(TimerEvent.TIMER, handleEnterFrame, false, 0, true);
snowTimer.start();
function handleEnterFrame(e:Event):void
{
var makeSnowflake = Math.random() * 100;
if(makeSnowflake > 80)
makeANewSnowflake();
for(var a:int = snowflakes.length - 1; a >= 0; a--)
{
snowflakes[a].y += snowflakes[a].height / 30;
snowflakes[a].rotation += snowflakes[a].scaleX;
if(snowflakes[a].y > 400 +snowflakes[a].height)
{
removeChild(snowflakes[a]);
snowflakes.splice(a,1);
}
}
}
function makeANewSnowflake():void
{
var flake:Snowflake = new Snowflake();
flake.scaleX = flake.scaleY = Math.random() * (1.2 - .1) + .1;
flake.x = Math.random() * (0 - 550) + 550;
flake.y = -flake.height;
flake.cacheAsBitmap = true;
addChild(flake);
var filt:BlurFilter = new BlurFilter(flake.scaleX * 3, flake.scaleY *3);
flake.filters = [filt];
snowflakes.push(flake);
}
function destroy():void
{
closeBtn.removeEventListener(MouseEvent.CLICK, handleCloseClick);
snowTimer.removeEventListener(TimerEvent.TIMER, handleEnterFrame);
snowflakes = null;
for(var a:int = 0; a < this.numChildren; a++)
{
try {
removeChildAt(0);
}
catch(e:*){}
}
}