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

  • Home
  • SEARCH
  • 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 6385375
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T02:53:29+00:00 2026-05-25T02:53:29+00:00

I understand how weak references work, but I am bit confused regarding it’s use

  • 0

I understand how weak references work, but I am bit confused regarding it’s use in actionscript event listeners. Consider the example below:

public class Rectangle extends MovieClip {
  public function Rectangle() {
    var screen:Shape=new Shape();
    screen.addEventListener(MouseEvent.MOUSE_OUT, new Foo().listen, false, 0, true);
    addChild(screen);
  }
}

public class Foo extends MovieClip {
  public function listen(e:MouseEvent):void {
    trace("tracing");        
  }        
}

Now here, since there is only a weak reference to Foo, would not the event listener Foo be garbage collected if and when the garbage collector runs and the code stop working as expected?

Is the weak event listener scenario prescribed only for event listener methods within the same class as below?

public class Rectangle extends MovieClip {
  public function Rectangle() {
    var screen:Shape=new Shape();
    screen..addEventListener(MouseEvent.MOUSE_OUT, listen, false, 0, true);
    addChild(screen);
  }

   public function listen(e:MouseEvent):void {
    trace("tracing");        
   }
 }

In the above scenario, is this how weak event listeners help?

If the Rectangle object has no other references, then it is a candidate for garbage collection, but since there is an event listener within the object, the event dispatcher holds a reference to the object, even though there are no other references to the object(other than the one held by the event listener). Hence it is prevented from being garbage collected. Is this the reason why weak event listeners are prescribed? Is the flash player so naive that, it cannot figure out that the event listener is defined within the same object?

  • 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-25T02:53:29+00:00Added an answer on May 25, 2026 at 2:53 am

    The basic rule for garbage collection in Flash is that if an object is referenced (directly or indirectly) from the timeline, it cannot be garbage collected. This means that if an instance of your class is not being referenced from anywhere in the root display list (or by an object that is in turn referenced from the root, et c recursively), it will be collected regardless of whether it has references to itself.

    This also means that two objects that reference each other, but are not being referenced in any way from the root display list, should be eligible for garbage collection.

    The weak reference option in event listeners is mainly so that you won’t have to remove the event listener manually. I personally tend not to use it, because I like to have full control of when objects get marked for garbage collection.

    So, in your first example, the Foo instance is in all cases eligible for garbage collection. In your second, whether or not the Rectangle instance can be garbage collected depends on whether it’s referenced from the display list. The useWeakReference flag in this regard makes no difference.

    From the documentation here:

    Class-level member functions are not subject to garbage collection, so you can set useWeakReference to true for class-level member functions without subjecting them to garbage collection. If you set useWeakReference to true for a listener that is a nested inner function, the function will be garbage-collected and no longer persistent. If you create references to the inner function (save it in another variable) then it is not garbage-collected and stays persistent.

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

Sidebar

Related Questions

I understand what System.WeakReference does, but what I can't seem to grasp is a
I understand that some countries have laws regarding website accessibility. In general, what are
I understand the overall meaning of pointers and references(or at least I think i
I understand the difference between String and StringBuilder ( StringBuilder being mutable) but is
I understand, in a fuzzy sort of way, how regular ACID transactions work. You
as i currently understand, if an event listener is added to an object with
I’m working in a pure iOS5/ARC environment, so I can use __weak references as
I understand how to use weak_ptr and shared_ptr . I understand how shared_ptr works,
I have a simple problem but I just don't understand any of the examples
I can't understand why it is correct to define a delegate with weak pointer

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.