this.parent.removeChild(this); and
this.parent.removeChild(this); are synonomous (except for popups, where you need to use this.parent.removeChild(this) ). Tom, you should also be aware that this removal of the instance from the DisplayList will only make the instance available for garbage collection if that's the only reference to the object. Effectively, you need to be very careful how you create the object and be sure to remove any references to it - or just use addChild(new xx() ). Paul ----- Original Message ----- From: Haykel BEN JEMIA To: flexcoders@yahoogroups.com Sent: Friday, November 28, 2008 12:01 PM Subject: Re: [flexcoders] How to get an object to delete itself? Setting lifeTimer to null shouldn't be necessary as lifeTimer is a private variable of DrawingTool and will not be referenced somewhere else thus the references between DrawingTool and lifeTimer are circular and the garbage collector handles these cases correctly (during the "mark and sweep" process). To remove the control from its parent's display list, you should use: this.parent.removeChild(this); Another method could be to dispatch an event when the timer fires, listen for it in the parent and remove the control in the event handler. Yet another suggestion: You could also implement a DrawingToolsManager which would create DrawingTool objects on demand, add them to a container and remove them when a timer event fires. Haykel Ben Jemia Allmas Web & RIA Development http://www.allmas-tn.com On Fri, Nov 28, 2008 at 12:25 PM, Paul Andrews <[EMAIL PROTECTED]> wrote: Tom, Setting alpha=0 won't make the object available for garbage collection. If you don't want the oject any more you need to remove all references to it. This will require that you remove it from the Display list and remove any other references to it. You also need to remove the timer. So your destruct method needs (at least): this.owner.removeChild(this); // remove this instance from the parent container in the display list lifeTimer = null; //remove the reference to it Paul ----- Original Message ----- From: tom s To: flexcoders@yahoogroups.com Sent: Friday, November 28, 2008 11:08 AM Subject: [flexcoders] How to get an object to delete itself? I have custom class that extends UIComponent which I use for putting graphics on to the stage (I add them as children of a seperate UIComponent). In the constructor I create a Timer and watch for the TIMER_COMPLETE event, at which point I want the graphic to dissapear, which I do by setting alpha = 0. However, I am concerned that it will not be garbage collected, and I could end up with quite a few of these, which may be bad for memory. Question: Can i get the object to delete itself? (or atleast be in a state suitable for grabage collection) e.g. this.selfDestruct() ;) The relevant code is shown below. thanks tom public class DrawingTool extends UIComponent { private var lifeTimer:Timer = new Timer(8000,1); public function DrawingTool():void{ super(); lifeTimer.addEventListener(TimerEvent.TIMER_COMPLETE,onLifeTimer); lifeTimer.start(); } private function onLifeTimer(e:Event):void{ this.alpha = 0 trace(">>>time out"); } }