Proper way to dispose of a display object with children & listeners
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.
