0 Replies Latest reply on Oct 16, 2007 9:54 AM by c_infanti

    SWFLoader Memory Leak

    c_infanti
      Hi guys,

      After some searching on the web can't seem to find an answer to this one...basically, I have an parent application that loads smaller applications using the SWFLoader component (called swfLoader). When you click on a drop down menu, it puts the name of the item on the menu into a string and runs the following function:

      public function loadContent(link:String):void
      {Application.application.swfLoader.load("content/" + link + ".swf");}

      However, what I have noticed (by monitoring using System.totalMemory) is that each time a new swf is loaded, memory usage goes up, until eventually the application crashes. I'm figuring I need to unload the swfs before loading a new one somehow, but no idea how.

      After some suggestions I have removed everything from all of the child swfs (so now they are just a blank canvas with no associated actionscript), and everything from the main swf with the exception of the functions needed to load the swf and a button to force run the garbage collector for testing purposes. So that leaves me with this in main.mxml:

      <?xml version="1.0" encoding="utf-8"?>
      <mx:Application xmlns:mx=" http://www.adobe.com/2006/mxml" layout="absolute"
      initialize="initTimer()" >
      <mx:Style source="css/main.css" />
      <mx:Script source="as/main.as" />

      <mx:Canvas width="100%" height="100%">

      <!-- Navigation -->
      <mx:HBox width="100%" height="65" styleName="glassPanel" horizontalAlign="center">

      <mx:PopUpMenuButton id="kpiBtn" itemClick="loadContent(event.item.data);" dataProvider="{KPIList}"
      toolTip="Access the KPIs"/>
      <mx:Button label="GC" click="GC();" styleName="printBtn" height="22"
      toolTip="Run Garbage Collector. "/>

      <mx:Label color="white" text="{Application.application.numFormat.format(totmem/1000000)} MB"/>
      <mx:Label color="white" text="{Application.application.numFormat.format(maxmem/1000000)} MB"/>


      </mx:HBox>

      <!-- Content Area -->
      <mx:HBox top="35" width="100%" height="100%" horizontalAlign="center">
      <mx:Canvas width="100%" height="100%" id="all" styleName="glassPods" >
      <mx:Box styleName="glassPanel" width="100%" horizontalAlign="right">
      <mx:Label id="labelEdition" styleName="glassLabels"/>
      </mx:Box>
      <mx:SWFLoader id="swfLoader" height="100%" width="100%" scaleContent="true"
      top="13" bottom="5" right="5" left="5" cacheAsBitmap="false"/>
      </mx:Canvas>
      </mx:HBox>

      </mx:Canvas>

      </mx:Application>

      And this in the main.as:

      import mx.controls.Menu;
      import mx.controls.SWFLoader;
      import mx.managers.PopUpManager;
      import mx.core.Container;
      import mx.printing.*;
      import flash.net.URLRequest;
      import flash.net.URLLoader;
      import flash.events.Event;
      import mx.controls.Alert;
      import mx.core.Application;
      import flash.display.DisplayObject;

      //Labels for the navigation PopUpMenuButton, data field is truncated file name
      public var KPIList: Array =[
      {label:"ISI Dashboard KPIs", data: "KPISplash"},
      {label:"Reading Levels", data: "ReadingKPI"},
      {label:"Benchmark Test Performance", data: "PerformanceKPI"},
      {label:"Benchmark Test Participation", data: "ParticipationKPI"},
      {label:"Instructional Support", data: "CSAPKPI"},
      {label:"Student Attendance", data: "AttendanceKPI"},
      {label:"Staff Absences", data: "HRKPI"},
      {label:"Student Suspensions", data: "SuspensionKPI"},
      {label:"Serious Incidents", data: "IncidentKPI"},
      ];


      public function loadContent(link:String):void
      {
      Application.application.swfLoader.load(link + ".swf"); // load new content
      }

      public function GC():void
      {
      try {
      new LocalConnection().connect('foo');
      new LocalConnection().connect('foo');
      } catch (e:*) {}
      }


      //Memory debug tools********************************************
      import flash.utils.Timer;
      import flash.events.TimerEvent;
      import mx.formatters.NumberFormatter;

      [Bindable]
      public var time:Number = 0;
      [Bindable]
      public var totmem:Number = 0;
      [Bindable]
      public var maxmem:Number = 0;
      [Bindable]
      public var numFormat:NumberFormatter = new NumberFormatter();




      public function initTimer():void {
      // The first parameter is the interval (in milliseconds). The
      // second parameter is number of times to run (0 means infinity).
      var myTimer:Timer = new Timer(1000, 0);
      Application.application.numFormat.precision = 2;
      myTimer.addEventListener("timer", timerHandler);
      myTimer.start();
      }

      public function timerHandler(event:TimerEvent):void {
      time = getTimer()
      totmem = flash.system.System.totalMemory;
      maxmem = Math.max(maxmem, totmem);
      }

      Finally, here's an example of one of the .swfs being loaded:

      <?xml version="1.0" encoding="utf-8"?>
      <mx:Application xmlns:mx=" http://www.adobe.com/2006/mxml" layout="absolute">


      <!--Application Control Bar*******************************************-->

      <mx:Canvas height="100%" width="100%" id="canvasAttendance" styleName="controlBarBox">
      <mx:Image source="assets/welcome_images/welcome0.jpg"/>
      </mx:Canvas>

      <!--Summary Panel*******************************************-->


      </mx:Application>

      Anyone have any ideas?

      Thanks!
      Chris