Exit
  • Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
  • 한국 커뮤니티
0

Instantiation, display objects and memory leaks question

Guest
May 20, 2013 May 20, 2013

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.

TOPICS
ActionScript
807
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines

correct answers 1 Correct answer

Community Expert , May 20, 2013 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.

Translate
Community Expert ,
May 20, 2013 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).

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Guest
May 20, 2013 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?

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
May 20, 2013 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.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Guest
May 26, 2013 May 26, 2013

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.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
May 26, 2013 May 26, 2013
LATEST

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.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines