Specifications
CHAPTER 4
74
<mx:Button id="b2" label="Jack"/>
<mx:Button id="b3" label="Sawyer"/>
</mx:VBox>
</mx:Panel>
<mx:Button id="myButton2" click="destroyButtons(event)" label="Destroy Buttons"/>
</mx:Application>
You can clear references to unused variables by setting them to null in your ActionScript; for example:
myDataProvider = null
To ensure that destroyed objects are garbage collected, you must also remove event listeners on them by using the
removeEventListener() method, as the following example shows:
<?xml version="1.0"?>
<!-- optimize/RemoveListeners.mxml -->
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml"
creationComplete="initApp(event)">
<mx:Script><![CDATA[
private function initApp(e:Event):void {
b1.addEventListener("click",myClickHandler);
b2.addEventListener("click",myClickHandler);
b3.addEventListener("click",myClickHandler);
}
private function destroyButtons(e:Event):void {
b1.removeEventListener("click",myClickHandler);
b2.removeEventListener("click",myClickHandler);
b3.removeEventListener("click",myClickHandler);
myVBox.removeChild(b1);
myVBox.removeChild(b2);
myVBox.removeChild(b3);
}
private function myClickHandler(e:Event):void {
// Do something here.
}
]]></mx:Script>
<mx:Panel title="VBox with Repeater">
<mx:VBox id="myVBox" height="100" width="125">
<mx:Button id="b1" label="Hurley"/>
<mx:Button id="b2" label="Jack"/>
<mx:Button id="b3" label="Sawyer"/>
</mx:VBox>










