4 Replies Latest reply on Oct 22, 2010 11:47 AM by Flex harUI

    Another problem with swfloader (subapplication) and gc

    kostis alexandris

      I am trying to load a swf sub-application using the swfloader. I know that this is a well-discussed topic but i cannot find the reason why the air gc does not get rid of the loaded subapplication or -in a better way- why does it get rid of some intances and other instances remain in the memory (cumulative instances 10, instances 4 never destroyed). Here is my code

      ParentApplication
      <?xml version="1.0" encoding="utf-8"?>
      <s:WindowedApplication xmlns:fx="http://ns.adobe.com/mxml/2009"
                             xmlns:s="library://ns.adobe.com/flex/spark"
                             xmlns:mx="library://ns.adobe.com/flex/mx"
                             width="500"
                             height="1000"
                             creationComplete="onCreationComplete()">
          <fx:Script>
              <![CDATA[
                  import mx.controls.Alert;
                  import mx.controls.SWFLoader;
                  private var loader:SWFLoader;
                 
                  private function onCreationComplete():void
                  {
                      addEventListener(MouseEvent.CLICK, loadSwf);
                      addEventListener(MouseEvent.RIGHT_CLICK, unloadSwf);
                      function loadSwf():void
                      {
                          loader = new SWFLoader();
                          loader.addEventListener(Event.UNLOAD,unloaded);
                          loader.cachePolicy = "off";
                          loader.width = 500;
                          loader.height = 1000;
                          loader.trustContent = false;
                          loader.loadForCompatibility = true;
                          addElement(loader);
                         
                          var mySubApplication:File = File.userDirectory.resolvePath("file:///C:/ReaGuide.swf");
                          loader.load(mySubApplication.url);
                      }
                      function unloadSwf():void
                      {
                          removeElement(loader);
                          var e:Event = new Event("subAppCleanUp");
                          loader.content.loaderInfo.sharedEvents.dispatchEvent(e);
                          loader.source = null;
                          loader.load(null);
                          loader.unloadAndStop(true);
                          loader = null;
                          System.gc();
                          System.gc();
                             
                      }
                      function unloaded():void
                      {
                          Alert.show("unloaded event")
                      }
                  }
              ]]>
          </fx:Script>
      </s:WindowedApplication>
      SubApplication
      <?xml version="1.0" encoding="utf-8"?>
      <mx:Application xmlns:mx="http://www.adobe.com/2006/mxml"
                      layout="absolute" backgroundColor="#333333"                
                      width="100%" height="100%" creationComplete="onCreationComplete()">
          <mx:Script>
              <![CDATA[
                  import mx.controls.Alert;
                  public function onCreationComplete():void{
                      this.systemManager.loaderInfo.sharedEvents.addEventListener("subAppCleanUp",_cleanUp)
                          function _cleanUp():void
                          {
                              trace("CleanUp called from parent");
                              myImage = null;
                          }
                  }
                 
              ]]>
          </mx:Script>
      <mx:Image id="myImage" source="@Embed(source='images/rea-00.jpg')" width="500" height="1000"/>
      </mx:Application>

       

      I would like to mention that if i remove the mx:Image from the subApplication tha garbage collector works fine loading and unloading the swf correctly.

       

      As you can see i have created an event (dispatched from the parent application ) that cleans up the subapplication in order to avoid garbages.

       

      i would be gratefull for any piece of advice.

      Thank you!