Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7029701
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T00:34:25+00:00 2026-05-28T00:34:25+00:00

I’m trying to have a global cleanup function, and pass the MovieClip or TextField

  • 0

I’m trying to have a global cleanup function, and pass the MovieClip or TextField for removal. I want to remove the clip without referencing the clip directly. Any help would be greatly appreciated.

var clip:Sprite = new Sprite();
clip.graphics.beginFill(0x000000, 1)
clip.graphics.drawRect(0,0,100,100)
clip.graphics.endFill()
clip.name = "clip"
addChild(clip);

cleanup(clip)

function cleanup(mc):void {
    removeChild(mc.parent.getChildByName(mc.name));
    mc = null

    // clip is removed and traces as [object Sprite]
    trace(clip)

    //
    addChild(clip);
    removeChild(clip)
    clip = null

    // clip is removed and traces as null.
    trace(clip)
}

Here is the FLA (Flash cs4): http://dl.dropbox.com/u/4725599/test-cs4.zip

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-28T00:34:25+00:00Added an answer on May 28, 2026 at 12:34 am

    You seem to have a misunderstanding of scope. I’ll try to explain what you’re doing:

    function cleanup(mc):void {
        removeChild(mc.parent.getChildByName(mc.name)); // removes the "clip" sprite
        mc = null; // nulls the local var "mc", which essentially does nothing
    
        addChild(clip);     // re-add the "clip" sprite
        removeChild(clip)   // remove the "clip" sprite
        clip = null;        // null the "clip" sprite, removing the last reference to the memory allocated for the sprite
    }
    

    Basically, when you create your Sprite, you’re allocating the memory needed for a Sprite and assigning it to the clip variable. Your sprite now has one reference to it.

    You then add it to the stage. This creates a new reference, so your sprite now has 2 references. That’s it.

    The mc variable in the cleanup() function is a temporary variable – they’re cleaned up after the function exits so you don’t need to worry about it. Basically to tag your Sprite for garbage collection, you need to remove the 2 references to it: the clip variable, and the stage. So you simply need to do:

    clip.parent.removeChild( clip );
    clip = null;
    

    And it’s done.

    Check out that: http://divillysausages.com/blog/tracking_memory_leaks_in_as3 (disclaimer: my site) which should give you a good explanation of how memory works in as3

    Edit
    Normally you would work this based on classes. In all my classes, I implement a destroy() function that I can call to clean up the class (remove children, event listeners, any references to other objects etc). For the code calling destroy(), it doesn’t care what the object does or how it works, it just know that afterwards the object is good for garbage collection.

    As a general principle, the object creating something is also responsible for destroying it.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
I have a French site that I want to parse, but am running into
I want to construct a data frame in an Rcpp function, but when I
I am trying to loop through a bunch of documents I have to put
I have a .ini file as follows: [playlist] numberofentries=2 File1=http://87.230.82.17:80 Title1=(#1 - 365/1400) Example
I am trying to understand how to use SyndicationItem to display feed which is
Basically, what I'm trying to create is a page of div tags, each has
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have just tried to save a simple *.rtf file with some websites and
I want to count how many characters a certain string has in PHP, but

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.