Flex Modules, Watch Your Scope!!
14 August 2008Here’s another help hint for those working with Modules in Flex: Watch your variable scope!!
When loading modules, you have a number of options for loading the SWF including the ModuleLoader and ModuleManager. If neither of those fit your needs you always have access to Flash’s Loader class. ModuleManger can be used for fine grained control of the loading process. Either way, variable scope plays a crucial roll in the loading process.
Here’s an example using ModuleLoader…
public function loadSWFModule(swfURL:String):void
{
var moduleLoader:ModuleLoader = new ModuleLoader();
moduleLoader.url = swfURL;
moduleLoader.addEventListener(ModuleEvent.READY,onModuleLoaded);
moduleLoader.addEventListener(ModuleEvent.ERROR,onModuleError);
moduleLoader.addEventListener(ModuleEvent.PROGRESS,onProgress);
moduleLoader.loadModule();
}
The moduleLoader is declared as a local variable. As it turns out this will cause problems, even with the ’strong’ event listeners. For those not wanting to read the technical details to follow, simply use avoid using a locally scoped variable.
The following are some of the symptoms you may run into using the latter locally scoped variable:
To help those searching the net, I’ll be a bit redundant in the ’symptom’ descriptions so hopefully you’ll find this post if you’ve stumbled upon this problem
Symptons
The problem comes in when the Loader finishes loading the SWF. ModuleInfo’s internal listener list (listeners attached during ModuleInfoProxy’s constructor) are lost (i.e. null) when ModuleInfo finally reaches it’s readyHandler( ) method. At the end of the readyHandler( ), a ModuleEvent.READY event is dispatched.
dispatchEvent(new ModuleEvent(ModuleEvent.READY));
With the listener list being null, the event is never picked up by the ModuleInfoProxy and the initial load fails.
On subsequent loads (i.e. the second time) ModuleInfoProxy’s load( ) method checks to see if the info has already been loaded and dispatches the ModuleEvent.READY event directly.
else if (info.loaded)
{
//trace("Module[", url, "] load is already loaded");
if (info.setup)
{
...
if (info.ready)
{
...
dispatchEvent(new ModuleEvent(ModuleEvent.READY));
}
}
}
Causing the load to ‘work’ the second time.
My understanding is that Garbage Collection is often invoked when a module is loaded. It looks like it’s the cause of this behavior (the listeners being lost). I assumed that the ’strong’ listeners would be enough to keep the GC away, oh well.
Again the solution is simple, make sure your not using a locally scoped variable when loading modules.










on September 12th, 2008 at 11:35 am
hi, thanks for your post Joshua.
for some reason i have to load many modules in the same time, and each instance need to initiate in the local variable scope.
And it seems it still works if I put the new instance of ModuleLoader into a Local array variable
Code as following:
private var loaderArr:Array = new Array();
public function LoadModuleFromSWF(path:String) : void
{
var loader:ModuleLoader;
loader = new ModuleLoader();
loader.url = path;
loader.addEventListener(ModuleEvent.READY, onModuleLoaded);
loader.addEventListener(ModuleEvent.ERROR, onModuleError);
loader.addEventListener(ModuleEvent.SETUP, onSetup);
loader.addEventListener(ModuleEvent.UNLOAD, onUnload);
loaderArr.push(loader);
loader.loadModule();
}
on September 18th, 2008 at 10:46 pm
Camus,
thanks, yes I’d imagine placing the loader in the array adds yet another reference to the local loader variable, keeping the GC at bay
on November 10th, 2008 at 7:18 pm
Incedentaly, we came across this in windows, but not Linux, so there is probably some difference in the garbage collection between the two flash players.
Thanks for the post though, it did solve our problem.