3
\$\begingroup\$

My point is this:

addChild(parentMC);
parentMC.addChild(child1);
parentMC.addChild(child2);

If I want to then remove parentMC from the current container, is it necessary to also remove child1 and child2 from parentMC in order to properly clean-up? Or is that handled automatically through removing parentMC?

\$\endgroup\$
1
\$\begingroup\$

Once you remove parentMC and set it as null, garbage collection will take care of its children.

When you remove the parent from the Flash display list hierarchy its children won't exist in the display list. You could remove children before you remove the parent from the display list, but that isn't necessary unless you wish to move the child to another container. But when you call addChild on the second parent (parentMC2.addChild(child1)) it will be automatically removed from parentMC. Assuming that parentMC does not hold any variables linking to the child1 instance, you are safe.

If your children listening to Event.REMOVED_FROM_STAGE and you would like to make a dispose function for your parent you could simply remove all the children from the parent one by one:

while( parent.numChildren > 0 ) parent.removeChildAt( 0 );

and then execute your dispose code after all the children have been removed. But yet again unless it's absolutely necessary for your program logic it would be a waste of time

\$\endgroup\$
  • \$\begingroup\$ One small caveat: if your child objects hold circular references to the parent or each other, this can prevent them from being immediately garbage collected until a full mark & sweep pass happens. So if you have complicated referencing going on in any of the child objects, take a quick look to see if any loops of references need to be broken when you remove the parent. It'll make the whole subtree a bit more digestible for the GC. ;) \$\endgroup\$ – DMGregory Mar 21 '15 at 13:36

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.