Garbage collection question

Subscribe to Garbage collection question 5 posts

avatar for MossyStump MossyStump 3193 posts
Flag Post

Alright! So, I’d like some clarification on garbage collection.

If I remove all references to an display object (container sprite), all of its children will be collected as well, given that none of them are tied past the parent?

Game → Display Object → Children

Display Object has reference to Game, but no children have reference to game.
So if I remove all references with Display Object, will the Children be collected as well?

derps a little

package
{
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.events.MouseEvent;
	
	[Frame(factoryClass="Preloader")]
	
	public class Main extends Sprite
	{	
		private var game:Game;
		private var menu:Menu = new Menu();
		public function Main():void
		{
			if (stage)
				init();
			else
				addEventListener(Event.ADDED_TO_STAGE, init);
		}
		
		private function init(e:Event = null):void
		{
			removeEventListener(Event.ADDED_TO_STAGE, init);
			stage.addEventListener(MouseEvent.RIGHT_CLICK, doNothing);
		
			addChild(menu);
		}
		
		private function doNothing(e:MouseEvent):void
		{
			
		}
		
		public function startGame():void
		{
			removeChild(menu);
			menu = null;
			
			game = new Game();
			addChild(game);
			
		}
		
		public function startMenu():void
		{
			removeChild(game);
			game = null;
			
			menu = new Menu();
			addChild(menu);
		}
	}

}

So will I have to go through and manually remove all listeners of the children of Game, or will the children be scrapped as well?
I’m pretty good with keeping my code clean and removing listeners and such when they’re not in use, but when It’s time to throw everything out (leave the game completely) it would just be easier to cut one string than go through and snip snip the remaining ones.

 
avatar for Senekis93 Senekis93 4090 posts
Flag Post

A listener is a reference.

 
avatar for MossyStump MossyStump 3193 posts
Flag Post
Originally posted by Senekis93:

A listener is a reference.

I know a listener is a reference, and I’ve removed all of the listeners in Game, I’m just wondering if I have to remove every listener in the children for them to be collected properly

 
avatar for NineFiveThree NineFiveThree 1378 posts
Flag Post
Originally posted by MossyStump:

I’m just wondering if I have to remove every listener in the children for them to be collected properly

As you’ve stated yourself, it depends on what you added the listener to.
As long as they are self referential or within the menu, you should be fine.

 
avatar for dragon_of_celts dragon_of_celts 295 posts
Flag Post
Originally posted by MossyStump:
Originally posted by Senekis93:

A listener is a reference.

I know a listener is a reference, and I’ve removed all of the listeners in Game, I’m just wondering if I have to remove every listener in the children for them to be collected properly

Weak-referenced listeners don’t need to be removed, from what I’ve read:
Stackoverflow Answer to Listener Reference Question

Adobe Docs (last paragraph under “Adding Event Listeners”)