Skip to main content
May 20, 2013
Answered

Instantiation, display objects and memory leaks question

  • May 20, 2013
  • 1 reply
  • 867 views

Hi

Just wondering if this would be a memory leak...?

In constructor...

- create a parent sprite (call it parentSPRITE)

- this.addChild(parentSPRITE)

- instantiate a display object from your Flash Pro lib (call it testMC)

In function...

- parentSPRITE.addChild(testMC)

In another function...

- this.removeChild(parentSPRITE);

- parentSPRITE = null;// what happens to testMC here?

Then call the first function again...

- parentSPRITE.addChild(testMC)

Does this mean multiple display testMC objects are building up in memory, or does testMC get erased from the display list each time it's parent is destroyed?

Thanks for your time and help.

This topic has been closed for replies.
Correct answer kglad

if you create one instance of mc and add it repeatedly to the same or different parents, nothing more than one instance of mc is created.

whether a parent is gc'd or not has not impact on memory allocated for the child unless there is no way to reference the child except via the parent.

1 reply

kglad
Community Expert
Community Expert
May 20, 2013

testMC still exists as long as it can be referenced.  removing an object from the display list does not prevent it from being referenced.

to make testMC eligible for gc, it should be removed from the display list, its listeners removed and then nulled (along with any other reference to it that you created).

May 20, 2013

What I meant to ask...

Does repeatedly using addChild(mc) build up multiple mc's in memory (from only one instantiation of it)? I'm guessing no, ie Flash ignores adding it if you've already added it. But if you add it to a parent, and then kill the parent, what happens then - I'm thinking that then mc is available for addChild again.

Better way to ask...

If you instantiate mc once, then addChild it to a sprite, then create a different sprite, can you addChild mc to the new sprite and it will also still be in sprite 1? ie there will be two mc display elements? Or is that only possible if you instantiate mc twice?

kglad
Community Expert
kgladCommunity ExpertCorrect answer
Community Expert
May 20, 2013

if you create one instance of mc and add it repeatedly to the same or different parents, nothing more than one instance of mc is created.

whether a parent is gc'd or not has not impact on memory allocated for the child unless there is no way to reference the child except via the parent.