How do I unload a externally loaded SWF file from a SWFLoader component in Adobe Flex?

15,948

Solution 1

... isn't that a problem with the Flex architecture?

Yes it is, and it also affects Flash in general. Until you can take advantage of the Loader.unloadAndStop() method in FP10 (AIR 1.5), you can't guarantee that externally loaded content will not continue to consume memory and cpu resources, even if you use the Loader.unload() method. (To be honest, I'm not 100% sure that even that will guarantee freeing of resources, but maybe I'm a pessimist.)

The next best thing is for you to insist that the creators of the content you load adhere to a set of guidelines, including exposing something like a dispose() method which your app can call to ask it to release as many resources as possible before you unload() it. If this isn't possible, then your application will almost definitely bloat in memory and cpu usage each time you load an external swf. Sorry.

If it makes you feel any better, you're not alone. ;)

Solution 2

It is a problem that a badly created SWF can sink your application, and many of the issues with this will be fixed in Flash Player 10, as others have mentioned. However, regardless of platform you will always risk having problems if you load third party code, there's always the possibility that it contains bugs, memory leaks or downright malicious code. Unless you can load content into a sandbox (and you can't in Flash, at least not yet), loading bad things will sink your app, it's as simple as that.

I'm sorry to say that unless you can guarantee the quality of the loaded content you can't guarantee the quality of your own application. Flash developers are notorious for writing things that leak, or can't be unloaded, because Flash makes it easy to do the wrong thing, especially for things that live on the time line. Loading any Flash content that you don't have control over directly is very perilous.

Solution 3

The best solution is

swfLoader.autoLoad = false;
swfLoader.unloadAndStop();
swfLoader.autoLoad = true;

In this way you stop the player, unload the content from memory and avoid the sound to remain playing.. Cheers

Solution 4

The problem resides in the loaded swf, it simply does not clean up the audio after itself. Try attaching an unload event onto movieclips like this:

MovieClip(event.target.content).loaderInfo.addEventListener(Event.UNLOAD, unloadMovieClipHandler);
private function unloadMovieClipHandler(event:Event) : void
{
  SoundMixer.stopAll();                           
} 
Share:
15,948
Shawn
Author by

Shawn

a long time ago i found an xss vuln here and took down the entire site userid 26, respect

Updated on August 26, 2022

Comments

  • Shawn
    Shawn over 1 year

    I have an application that loads external SWF files and plays them inside a Adobe Flex / Air application via the SWFLoader Flex component. I have been trying to find a way to unload them from a button click event. I have Google'd far and wide and no one seems to have been able to do it without a hack. The combination of code I see people use is:

    swfLoader.source = ""; // Removes the external link to the SWF.
    swfLoader.load(null); // Forces the loader to try to load nothing.
    // Note: At this point sound from the SWF is still playing, and
    // seems to still be playing in memory.
    flash.media.SoundMixer.stopAll();
    // Stops the sound. This works on my development machine, but not 
    // on the client's.
    

    If the SWFs are closed (hidden) this way, eventually the program crashes.

    Any ideas? I have found tons of posts in various forums with people having the same problem. I assume I will get one wrong/incomplete answer here, and than my post will sink into nothingness as usual, but either way, thanks in advance!

    Edit 1: I can't edit the actual SWF movies, they're created by the client. If I can't close any SWF opened through Flex, isn't that a problem with the Flex architecture? Is my only option sending the SWFs to the web browser?

  • Shawn
    Shawn over 15 years
    I'm not using a Loader, I'm using the Flex SWFLoader component. I'll give the Loader class a try tomorrow morning though. Thanks...
  • aaaidan
    aaaidan over 15 years
    That GC hack doesn't seem like a best practice to me. I'd only recommend using it in a development environment to help get a more meaningful memory usage value. Relying on it in production might be something you regret later.
  • buddyp450
    buddyp450 about 11 years
    Actually found this post after Loader.unloadAndStop() failed to work. No hope in sight!
  • aaaidan
    aaaidan about 11 years
    Odd. Can you elaborate? Maybe even ask a question if one doesn't already exist!