Exit
  • Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
  • 한국 커뮤니티
0

Proper way to dispose of a display object with children & listeners

Explorer ,
Oct 15, 2013 Oct 15, 2013

This is my function that creates a display object with multiple children, one of which has an event listener.

function makeIt():void {

var spriteA:Sprite = new Sprite();

var spriteB:Sprite = new Sprite();

spriteB.addEventListener(MouseEvent.CLICK, myCallback);

var spriteC:Sprite = new Sprite();

spriteA.addChild(spriteB);

spriteB.addChild(spriteC);

stage.addChild(spriteA);

}

Let's say I'm done with it. What's the proper way to dispose of it so that it no longer uses up memory? Do I have to do all this?

spriteB.removeChild(spriteC);

spriteB.removeEventListener(MouseEvent.CLICK, myCallback);

spriteA.removeChild(spriteB);

stage.removeChild(spriteA);

I've read some people say that you should also assign things to null, so maybe like this?:

spriteB.removeChild(spriteC);

spriteC = null;

spriteB.removeEventListener(MouseEvent.CLICK, myCallback);

spriteA.removeChild(spriteB);

spriteB = null;

stage.removeChild(spriteA);

spriteA = null;

And I've got a few more questions:

1. What happens if I just removeChild(spriteA) when I'm done with it and call makeIt() again later when I need it? Are the same variables being reassigned, so there's no need to clean up after them?

2. If this display object is going to be frequently used and removed, would it be better to just toggle spriteA.visible?

Also, I'm familiar with weak event listeners, but I'd prefer not to use them.

TOPICS
ActionScript
783
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines

correct answers 1 Correct answer

LEGEND , Oct 15, 2013 Oct 15, 2013

removeChild is not disposing of anything, it is merely removing it from the display list... it could be deemed very similar to setting an object's visible property to be false as far as visuals go... the object is still around and can be targeted with code in both cases.  If you want things to go away and be ready for garbage collection then you need to remove all references to them and null the objects.  If you know that an object will be reused frequently then you probably want to go the visib

...
Translate
LEGEND ,
Oct 15, 2013 Oct 15, 2013

removeChild is not disposing of anything, it is merely removing it from the display list... it could be deemed very similar to setting an object's visible property to be false as far as visuals go... the object is still around and can be targeted with code in both cases.  If you want things to go away and be ready for garbage collection then you need to remove all references to them and null the objects.  If you know that an object will be reused frequently then you probably want to go the visible true/false route, though using removeChild/addChild can work as well.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Explorer ,
Oct 15, 2013 Oct 15, 2013

So in my example, would this be the bare minimum to remove all the references?

spriteB.removeEventListener(MouseEvent.CLICK, myCallback);

stage.removeChild(spriteA);

spriteA = spriteB = spriteC = null;

And would delete() be better so you don't have variables that do nothing but hold null?

spriteB.removeEventListener(MouseEvent.CLICK, myCallback);

stage.removeChild(spriteA);

delete(spriteA);

delete(spriteB);

delete(spriteC);


Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Oct 15, 2013 Oct 15, 2013
LATEST

Do a quick search of Google using "AS3 delete vs null" and read thru the first few results... it'll save me from repeating it to you.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines