0 Replies Latest reply on Nov 28, 2007 11:13 AM by Quatrol

    Strange Memory Behavior With Popups

      Hello,

      I have been profiling my flex 2 application with flex builder 3 (but using flex 2 hotfix 3 SDK to compile) to find out memory leaks and i have seen a strange memory behavior when playing around with popups.

      To better isolate the problems i was having, I made a simple application that mimics the overall logic of my application and that is where i saw the strange problem : my component leaks, but only once; if i recreate the component, the previous instance gets collected and the component will not leak anymore.

      The simple application is comprised of the main app and 2 components ( PupupMenuMemoryTest and PopupMenu) (Will provide the code farther down). The main app contains 2 buttons ( create and remove). The create button instantiate a PupupMenuMemoryTest and add it to the main application container while the remove button destroy that child. The PupupMenuMemoryTest component has only one button, which creates a PopupMenu object and pops it. The PopupMenu object is a component wrapping the PopupManger's createPopup and removePopup calls.

      Has anyone ever seen such behavior ? Or do anyone understands why it does that ? What bugs me most, is that it only happens the first time a popup is created, and the object to get stuck is the MemoryTestComponent, and not the popup itself !

      Steps to reproduce :
      1 - Start the application
      2 - Take a memory snapshot
      3 - Click "create" button, then the "create popup" button
      4 - Click out of the popup to get it closed, that click "remove" button
      5 - Take another memory snapshot
      6 - Check for loitering objects, see that the MemoryTestComponent is stuck in memory
      7 - Repeat steps 3,4 and 5
      8 - check for loitering objects between snapshots 1 and 3 and see that no components are stuck in memory
      9 - Repeat ad nauseum; the leak will not be reproduced until you restart the application

      --------------- Main application's code ---------------------

      <?xml version="1.0" encoding="utf-8"?>
      <mx:Application xmlns:mx=" http://www.adobe.com/2006/mxml"
      layout="vertical" horizontalAlign="center" verticalAlign="middle">
      <mx:Script>
      <![CDATA[
      import mx.controls.Text;
      import mx.collections.ArrayCollection;

      private var bool: Boolean = false;

      private function button1Handler():void
      {
      if (!bool)
      {
      bool = !bool;
      var dummy: PopupMenuMemoryTest = new PopupMenuMemoryTest();
      dummy.name = "dummy";

      box.addChild(dummy);
      }
      }

      private function button2Handler():void
      {
      if (bool)
      {
      bool = !bool;
      var dummy: PopupMenuMemoryTest = box.getChildByName("dummy") as PopupMenuMemoryTest;
      box.removeChild(dummy);
      }
      }

      ]]>
      </mx:Script>

      <mx:HBox id="MyBox">
      <mx:Button id="MyButton1" label="add" click="button1Handler()"/>
      <mx:Button id="MyButton2" label="remove" click="button2Handler()"/>
      </mx:HBox>
      <mx:HBox id="box"/>
      </mx:Application>

      --------------- PopupMenuMemoryTest component's code ---------------------

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

      <mx:Script>
      <![CDATA[
      import mx.core.Application;
      import com.taleo.vanguard.Vanguard;
      import mx.managers.PopUpManager;

      private function buttonClickHandler(pEvent: MouseEvent):void
      {
      var popup: PopupMenu = new PopupMenu();
      popup.popup(this);
      }

      ]]>
      </mx:Script>

      <mx:Panel>
      <mx:Button id="popB" label="create a popup" click="buttonClickHandler(event)" />
      </mx:Panel>
      </mx:VBox>

      --------------- PopupMenu component's code ---------------------

      <?xml version="1.0" encoding="utf-8"?>
      <mx:VBox xmlns:mx=" http://www.adobe.com/2006/mxml" height="200" width="200">
      <mx:Script>
      <![CDATA[
      import mx.events.FlexMouseEvent;
      import mx.managers.PopUpManager;

      public function popup(pCenterReference: DisplayObject): void
      {
      PopUpManager.addPopUp(this, pCenterReference, false);
      PopUpManager.centerPopUp(this);
      this.addEventListener(FlexMouseEvent.MOUSE_DOWN_OUTSIDE, applicationClickHandler);
      }

      public function close(): void
      {
      this.removeEventListener(FlexMouseEvent.MOUSE_DOWN_OUTSIDE, applicationClickHandler);
      PopUpManager.removePopUp(this);
      }

      private function applicationClickHandler(pEvent: Event): void
      {
      close();
      }
      ]]>
      </mx:Script>

      <mx:Panel>
      <mx:Text text="popup's text"/>
      </mx:Panel>
      </mx:VBox>