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 6943507
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T13:09:55+00:00 2026-05-27T13:09:55+00:00

Would this cause memory leak ? var mc:MovieClip ; //<<<<<<< OUTSIDE LOOP for (

  • 0

Would this cause memory leak ?

var mc:MovieClip ; //<<<<<<< OUTSIDE LOOP

for ( var i=0 ; i< 1000 ; i++)
{
   mc = new MovieClip() ;
   mc.addEventListener( MouseEvent.CLICK , onClick) ;
}

And what about this ?

for ( var i=0 ; i< 1000 ; i++)
{
  var mc:MovieClip ; //<<<<<<< INSIDE LOOP
   mc = new MovieClip() ;
   mc.addEventListener( MouseEvent.CLICK , onClick) ;
}

“removeEventListener” is not used in any of the above code, so I think that both are causing a memory leak.

  • 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-27T13:09:56+00:00Added an answer on May 27, 2026 at 1:09 pm

    Your 1000 Movieclips will have a reference to you onClick function. Not the other way around.
    So if you question is if your 1000 Movieclips will get GCed: They will eventually, if they don’t have any other reference.

    On the other hand the reference in your movieclips to your onClick function will keep that alive (and the object it might belong to). If those MCs have any other reference that will keep them alive.

    The following code:

    mc.addEventListener(MouseEvent.CLICK , function(ev:Event):void{  trace("I am only a poor anonymous function");  }, false, 0, true);
    

    Will have your listener function GCed pretty soon, as it doesn’t have any strong reference.

    setting useWeakReference to true can be pretty relevant if your adding an Eventlistener to your stage

    stage.addEventListener(MouseEvent.CLICK, someObjectBelowIntheDisplayList.listenerFunction);
    

    The above code will keep the Object with your listener Function alive, even if it has no other reference.

    someObjectBelowIntheDisplayList.addEventListener(MouseEvent.CLICK, stage.onClick)
    

    the above code will not keep your someObjectBelowIntheDisplayList alive. It has a reference to the stage, but the stage does not get a reference to someObjectBelowIntheDisplayList

    Edit: Please try the following code:

    import flash.display.MovieClip;
    import flash.events.Event;
    
    var mc:MovieClip ; //<<<<<<< OUTSIDE LOOP
    
    function enterframe(ev:Event):void
    {
        for ( var i=0 ; i< 1000 ; i++)
        {
           mc = new MovieClip() ;
           mc.onClick = function(ev:Event){};
           // Use one of the following lines, comment out the other one
           //mc.addEventListener( MouseEvent.CLICK , onClick) ; // no memory leak
           stage.addEventListener(MouseEvent.CLICK, mc.onClick); // memory will rise up and up
        }
    }
    
    this.addEventListener(Event.ENTER_FRAME, enterframe);
    
    function onClick(ev:Event):void
    {
    
    }
    

    This code clearly supports what I am saying: Using mc.addEventListener will not get the memory consumption up. It will stay around 20MB on my system.
    When using the line with the stage.addEventListener and using mc.onClick as the listener function, memory consumption will rise every frame.

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

Sidebar

Related Questions

On iPhone.. Why would code such as this cause memory leak? after 2 minutes
Can the following code cause a memory leak? Would using a StringBuffer actually improve
I feel that a piece of code I have would cause a memory leak.
What would be the best practice for sharing localization object (in this case ResourceBundle,
What logging library or approach would you recommend for this case: We want to
This would seem to be the case in Firefox 3.5+, there I can instantiate
This is what I thought would be a simple select clause, however the following
Would this work on all platforms? i know windows does \r\n, and remember hearing
Would this be a proper way to dispose of a BackGroundWorker? I'm not sure
Under what circumstances would this or would this not be safe? I have a

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.