2 Replies Latest reply on Apr 9, 2010 4:12 PM by Flex harUI

    Do I need to worry about these event handlers in a grid from a memory leak perspective?

    rickcr Level 1

      I'm pretty new to Flex and coudn't figure out how to add event handlers to inline item renderer components from the containing file script so I attached the listnerers simply as part of the components themselves (eg <mx:Checkbox ... chnage="outerDocument.doSomething(event)"../>):

       

      <mx:DataGrid id="targetsGrid" width="100%" height="100%" doubleClickEnabled="true" styleName="itemCell"
            headerStyleName="headerRow" dataProvider="{targets}"
            rowHeight="19" fontSize="11" paddingBottom="0" paddingTop="1">
           <mx:columns>
           <mx:DataGridColumn width="78" dataField="@isSelected" headerText="">
           <mx:itemRenderer>
                <mx:Component>
                     <mx:HBox width="100%" height="100%" horizontalAlign="center">
                          <mx:CheckBox id="targetCheckBox" selected="{data.@isSelected == 'true'}" 
                               change="outerDocument.checkChangeHandler(event);"/>
                          <mx:Image horizontalAlign="center" toolTip="Delete" source="@Embed('/assets/icons/delete.png')" useHandCursor="true" buttonMode="true" 
                               click="outerDocument.deleteHandler(event);"/>
                          <mx:Image id="editButton" horizontalAlign="center" toolTip="Edit" source="@Embed('/assets/icons/edit-icon.png')" useHandCursor="true" buttonMode="true" 
                               click="outerDocument.editHandler(event);"/>
                     </mx:HBox>
                </mx:Component>
           </mx:itemRenderer>
           </mx:DataGridColumn>
                <mx:DataGridColumn id="Name" dataField="@collectionDesc" headerText="Name" itemRenderer="com.foobar.integrated.media.ui.component.CellStyleForTargetName"/>
                <mx:DataGridColumn id="Created" width="140" dataField="@createDateTime" headerText="Created"  labelFunction="UiHelper.gridDateFormat" />
           </mx:columns>
      </mx:DataGrid>

       

      This grid is part of a view that will get destroyed and recreated potentially many times during a user's session within the application (there's a large dynamic nature to the app and views are created at run-time.) By destroyed I mean that the view that holds the above datagrid will no longer be referenced under certain circumstances and an entire new view object is created (meaning the old datagrid is no longer refernced and a new one is created.)

       

      I heard you should clean up event handlers when they are no longer used, and I know at what point the view is destroyed, but I don't know how to clean up the event handlers added to the item renderer components? Is it something that the Flex garbage collector will handle efficiently?

       

      Also, on a somewhat related note, how could I push the item renderer to an external component since in my event handlers for the item renderer buttons I need a reference to the datagrid.selectedIndex which, as an external item renderer I wouldn't have access to this containing grid?

        • 1. Re: Do I need to worry about these event handlers in a grid from a memory leak perspective?
          msakrejda Level 4

          No. You don't need explicit cleanup in this case: if your outerDocument is going away, you have nothing to worry about. The event handler leak can happen in sort of the reverse situation: suppose you have a long-lived MyView that contains a custom DataGrid like the one below. Now suppose that MyView frequently destroys and re-creates the grid. And suppose that on its creationComplete event, the grid registers a listener for outerDocument's (MyView's) enterFrame Event. Unless you explicitly remove this listener, MyView will still have a reference to it even after the grid that registered the listener is destroyed (and garbage collected).

           

          This is a pretty contrived example, but it sort of illustrates the potential for leaks: a certain component is elligible for garbage collection, but some longer-lived component holds a reference to it (or part of it, such as a listener function). If the longer-lived component is elligible for GC as well, you don't really need to worry about proper cleanup. That's what you're paying the GC processor cycles for.

          1 person found this helpful
          • 2. Re: Do I need to worry about these event handlers in a grid from a memory leak perspective?
            Flex harUI Adobe Employee

            Let's be careful here.  It is true that cleanup is not needed, but in the

            code snippet provided, outerDocument is implied to be a .mxml file that

            contains the DataGrid and its in-line components because the references from

            the renderer are to a function in a script block that is not defined within

            the DataGrid. 

             

            Even if the outerDocument is not destroyed and only the renderers are being

            created and destroyed, the code as written will not cause a leak because the

            renderer is referencing the outerDocument and not the other way around.

             

            And that is because the code in an event handler block is not a request to

            add an event listener to outerDocument, it is simply a method body of a

            handler attached to an object in the renderer.

             

            What would cause a leak is a call to outerDocument.addEventListener.