R – Childs and parents and the famous garbage collector in actionscript-3

actionscript-3garbage-collectionparent-child

Once again a question about the garbage collector in actionscript-3:

If I have a master container:

var masterContainer:Sprite = new Sprite();

And then I add a child to this container:

var childImage:Sprite = new Sprite();
masterContainer.addChild(childImage);
addChild(masterContainer);

And, I then decided to let the garbage collector collect the master container and all it's contents, will this be enough?

removeChild(masterContainer);
masterContainer = null;

Or do I have to store all child images' references somewhere just to be able to remove their childs from the master container later?

Also, would it be possible to let the garbage collector log a message when it deletes something and what exactly it is deleting? Somekind of event maybe?

Best Answer

The first part of your question:

Technically speaking it is enough. Although this depends on the side effects. If you have non-weak listeners nothing will be GCed.

Second part:

You can have a Dictionary with the object you want to monitor as a weak key. Then run a timer and see when this is deleted.

Related Topic