Abdulla
Abdulla

Reputation: 1127

AS3: Weak References Must Be Removed?

Do weakly referenced listeners have to be removed in order for garbage collection to occur? Is it good practice to remove the listeners even if they are weakly referenced or can we count on the garbage collector to deal with them appropriately?

Upvotes: 1

Views: 751

Answers (3)

chubbsondubs
chubbsondubs

Reputation: 38789

You don't have to remove weak listeners manually in order for them to be garbage collected. Not unregistering listeners cause most of the memory leaks you'll encounter so using weak listeners can dramatically improve your programs stability.

However, to keep a weak listener from getting removed before you are ready, you have to have at least one strong reference to it (or use an instance method). That usually means whatever registered the listener needs to hold onto it until that parent class references have been removed. That makes the listener last as long as the instance owning the listener which is the most common case with components.

There are cases where you'll have to manage listeners and that is if you plan on not listening for the entire life of the parent. Possibly where you want to remove the listener then add it back at a later time, not common, but it happens. That's where weak listeners don't have any advantage over strong reference listeners.

The garbage collector will remove weak listeners quickly once the strong reference to it is removed. Weak listeners make cleaning up much simpler. While people are cautious, as I'm sure you'll hear, because weak listeners are fairly new to UI frameworks. When GC was added to mainstream languages most people were overly cautious about that too.

We rely on the GC to clean up memory, that works and makes for much stabler code, and these days people don't call that sloppy code. Weak listeners aren't any different.

Upvotes: 1

Mattias
Mattias

Reputation: 3907

I never use weak references myself but I am really careful to remove all listeners when I don´t need them anymore. I think it is better getting in the habit of always removing listeners rather than using weak references.

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.

Clarifications regarding weak references in actionscript listeners

http://gingerbinger.com/2010/07/actionscript-3-0-events-the-myth-of-useweakreference/

Upvotes: 0

adpalumbo
adpalumbo

Reputation: 3031

That's exactly the point of weakly referenced listeners -- you don't strictly need to remove them up for garbage collection to occur.

However, this feature shouldn't be abused as a means to write sloppy code. You should only rely on them in cases where you won't be able properly clean up after yourself. If you can anticipate when you'll be done listening for an event, you should use a strongly referenced event listener and remove it when you're done.

Upvotes: 0

Related Questions