# DisplayObjects and their Children

**URL:** <https://forum.kirupa.com/t/displayobjects-and-their-children/253515>\
**Category:** flash\
**Created:** [March 2, 2008, 10:26am UTC](https://forum.kirupa.com/t/displayobjects-and-their-children/253515 "2008-03-02T10:26:12Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![modifier](https://avatars.discourse-cdn.com/v4/letter/m/ac8455/32.png) [@modifier](https://forum.kirupa.com/u/modifier)\
**Post date:** [March 2, 2008, 10:26am UTC](https://forum.kirupa.com/t/displayobjects-and-their-children/253515/1 "2008-03-02T10:26:12Z")

</div>

as mentioned in many others threads we have to remove all objects we created manually.  
if the DisplayObject has eventlisteners you have to remove them manually, too.

for example:

```php
var circle:Sprite = new Sprite();
circle.addEventListener(Event.ENTER_FRAME, doSomething);
circle.addEventListener(Event.REMOVED_FROM_STAGE, doCleanup);

addChild(circle);

stage.addEventListener(MouseEvent.CLICK, doAction);

function doSomething(event:Event):void {
     trace('enterFrame event running');
}

function doAction(event:MouseEvent):void {
    trace('removing DisplayObject');
    removeChild(circle);
    circle = null;
}

function doCleanup(event:Event):void {
    trace('removing eventlisteners');
    circle.removeEventListener(Event.ENTER_FRAME, doSomething);
}

```

so i am still a bit confused about removing objects from the stage.  
what happens to a child object, if its parent has been removed from the stage?

```php
var circle1:Sprite = new Sprite();
var circle2:Sprite = new Sprite();

circle1.addEventListener(Event.ENTER_FRAME, doSomething);
circle1.addEventListener(Event.REMOVED_FROM_STAGE, doCleanup);
circle2.addEventListener(Event.REMOVED_FROM_STAGE, stillOnStage);

addChild(circle1);
circle1.addChild(circle2);

stage.addEventListener(MouseEvent.CLICK, doAction);

function doAction(event:MouseEvent):void {
    trace('removing DisplayObject');
    removeChild(circle1);
    circle1 = null;
}

function doSomething(event:Event):void {
     trace('enterFrame event running');
}

function stillOnStage(event:Event):void {
     trace('seems i was removed?');
}

function doCleanup(event:Event):void {
    trace('removing eventListener');
    circle1.removeEventListener(Event.ENTER_FRAME, doSomething);
}

```

so does circle2 still remain on the stage? is it still in the memory? will it get garbage collected? the listener tells me, that it was removed.

regards  
michael

Edit:  
[similar article](http://www.kirupa.com/forum/showthread.php?t=290524)

Edit:

guess the listeners for the removement should also be removed?

```php
function doCleanup(event:Event):void {
    trace('removing eventListener');
    circle1.removeEventListener(Event.ENTER_FRAME, doSomething);
    circle1.removeEventListener(Event.REMOVED_FROM_STAGE, doCleanup);
    circle2.removeEventListener(Event.REMOVED_FROM_STAGE, doCleanup);
}

```
