Copy link to clipboard
Copied
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.
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.
Copy link to clipboard
Copied
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).
Copy link to clipboard
Copied
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?
Copy link to clipboard
Copied
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.
Copy link to clipboard
Copied
So adding that one instance to a parent mulitple times results in multiple visiual copies though?
A good practical example is I have 10 instances of a 'TOP' button that users can click to get to the top of the page. Since they all do exactly the same thing (scroll the mc to zero) if I can get away with having just one instance instead, that would be more memory cost effective.
Copy link to clipboard
Copied
yes, 10 instances will use more memory than one instance. so, if you can use 1 instance (and perhaps move it so it's always visible), that would use less memory than having 10 instances located at various places.
Find more inspiration, events, and resources on the new Adobe Community
Explore Now