Removing Children of Containers
As everybody knows, not keeping track of referenced objects is BAD. And the one thing worse than not keeping track of referenced objects is dead references.
For example, if you had an instance of an object, you must dereference everything associated with it before it is garbage collected. This may seem like child’s play to most veterans; things like event listeners are easily removed. However, it is important to note that in the case of DisplayObjects, your DisplayObject may have children that are also referencing it (through the ‘parent’ property). Removing the DisplayObject at this stage will therefore result in dead references to its children and itself! You’ll end up with massive amounts of memory leak as time goes along.
Luckily, there is a handy Event you can use. The Event.REMOVED Event fires when an object is removed from the display list! So with a bit of code, one can easily remove all children when it is removed from the display list.
this.addEventListener(Event.REMOVED, iveBeenRemoved, false, 0 , false);
private function iveBeenRemoved(e:void):void
{
while (this.numChildren != 0)
{
this.removeChildAt(0);
}
this.removeEventListener(Event.REMOVED, iveBeenRemoved, false);
}
Simply one of the things we might overlook in our coding practices. I myself will try to remember about child objects.





