1 Reply Latest reply on Jul 23, 2012 9:58 AM by Flex harUI

    Flex Spark ItemRenderer Memory leak!!

    zxysnow

      My Code:

      Here is ths Main.MXML

      <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"

                                                           creationComplete="iniApp()">

                <fx:Declarations>

                          <!-- 将非可视元素(例如服务、值对象)放在此处 -->

                </fx:Declarations>

                <s:VGroup width="100%" height="100%">

                          <s:HGroup >

                                    <s:Button label="add" click="onAdd()"/>

                                    <s:Button label="remove" click="onRemove()"/>

                                    <s:List width="100%" height="100%" id="lt"

                                                        itemRenderer="TestRender"/>

                          </s:HGroup>

                </s:VGroup>

                <fx:Script>

                          <![CDATA[

                                    import mx.collections.ArrayCollection;

                                    [Bindable]

                                    private var dataProvider:ArrayCollection=new ArrayCollection();

                                    private function iniApp():void

                                    {

                                              dataProvider.addItem({name:"HH"});

                                              dataProvider.addItem({name:"XX"});

                                              dataProvider.addItem({name:"ZZ"});

                                    }

       

                                    private function onAdd():void

                                    {

                                              lt.dataProvider=dataProvider;

                                    }

       

                                    private function onRemove():void

                                    {

                                              lt.dataProvider=null;

                                    }

                          ]]>

                </fx:Script>

      </s:WindowedApplication>

       

       

      Here is the TestRender.MXML

       

      <s:ItemRenderer xmlns:fx="http://ns.adobe.com/mxml/2009"

                                              xmlns:s="library://ns.adobe.com/flex/spark"

                                              xmlns:mx="library://ns.adobe.com/flex/mx"

                                              autoDrawBackground="true" creationComplete="createComp()">

       

                <s:Button label="{btnName}"/>

                <fx:Script>

                          <![CDATA[

                                    private function createComp():void

                                    {

                                              trace("create",this);

                                    }

                                    [Bindable]

                                    private var btnName:String="";

       

                                    override public function set data(value:Object):void

                                    {

                                              super.data=value;

                                              if(value)

                                              {

                                                        btnName=value.name;

                                              }

                                    }

       

                          ]]>

                </fx:Script>

       

      </s:ItemRenderer>

       

      When I click the add button,the TestRender will be created 4 times,but the dataProvider's length is 3.Why?

      And if I run this in the profile, click remove button,force GC,there is still one TestRender object in memory.

      Who can tell me why? Thanks!