The key is which SWFs code is calling load() on the module.  If you want your 
plugins to unload, you'll need to use

new ApplicationDomain(Application.currentDomain)

but the code that makes that call cannot be in a class that is linked into the 
main application.  See the modules presentation on my blog for visuals of how I 
think about AppDoms.  The presentation won't explain your exact problem, but it 
might help you figure out why things are going funky.

Also make sure assetModule is an instance variable and not a local variable.  
That's discussed in another post on my blog.

Alex Harui
Flex SDK Developer
Adobe Systems Inc.<http://www.adobe.com/>
Blog: http://blogs.adobe.com/aharui

From: [email protected] [mailto:[email protected]] On Behalf 
Of [email protected]
Sent: Monday, April 27, 2009 5:12 AM
To: [email protected]
Subject: [flexcoders] Nested loading of SWF files





Hi folks,

I've been battling this problem for a while now and I'm at my wits' end.

I'm trying to implement a plugin system for our application, and having a devil 
of a time getting SWF file which was dynamically loaded itself, load additional 
SWF files.

It goes something like this:

1. Main Application Shell loads...
2. ---------+ Application loads...
3. -----------------+Plugin(s)

We have no problem getting app #1 to load app #2

However, try as I might, I cannot get app #2 to load and instantiate #3

I've tried various permutations using the ModuleManager, but this is the 
closest I get. When the onLoadComplete method get invoked, I can see that the 
SWF loaded, however the factory always returns NULL.

What is interesting is that when I extract this out in its own application, it 
works fine. This issue is triggered by the fact that I'm loading Plugin from a 
SWF that was loaded dynamically itself.

I believe this is due to the ApplicationDomain, but I cannot make heads or 
tails of it. I tried specifying currentDomain, new 
ApplicationDomain(Application.currentDomain) and new ApplicationDomain() 
without success.

Also, it is important to note that I cannot make reference a hard reference to 
the Foo class in either applications since by their nature, we will not know 
ahead of time what they will contain.

Googlin' around, this seems to be a fairly known problem, but I have not found 
a (clear) solution yet.

Any idea/help ? Thanks a bunch !
-Phil

assetModule = ModuleManager.getModule("Foo.swf");
assetModule.addEventListener(ModuleEvent.READY, onLoadComplete );
assetModule.addEventListener(ModuleEvent.ERROR, onLoadError);
assetModule.load();
.
.
.
private function onLoadComplete( event:Event ):void
{
trace("module loaded");

_pluginInstance = assetModule.factory.create() as Plugin;
if( _pluginInstance )
_pluginInstance.startup();
else
Alert.show("unable to instantiate module");
}

private function onLoadError( event:Event ):void
{
Alert.show("error");
}

My Plugin looks like this:

package
{
import mx.collections.ArrayCollection;
import mx.modules.ModuleBase;

public class Plugin extends ModuleBase

public function startup():void
{

}
.
.
.
}

and

package
{
import Plugin;

import mx.collections.ArrayCollection;
import mx.controls.Alert;
import mx.modules.ModuleBase;

public class Foo extends Plugin
{
public function Foo()
{
trace("foo constructor invoked");
}

override public function startup():void
{
trace("foo started");
}
.
.
.
}

Reply via email to