When to Use Weak References
Understanding when to use a weak references, versus strong references can be a confusing one. It took me awhile to fully grasp when, and when not to use one over the other. On top of that, there’s very little documentation on the LiveDocs about the subject. Under the documentation for IEventDispatcher.addEventListener(), it states:
useWeakReference:Boolean (default = false) — Determines whether the reference to the listener is strong or weak. A strong reference (the default) prevents your listener from being garbage-collected. A weak reference does not.
And here’s what they have listed for the Dictionary’s constructor:
weakKeys:Boolean (default = false) — Instructs the Dictionary object to use “weak” references on object keys. If the only reference to an object is in the specified Dictionary object, the key is eligible for garbage collection and is removed from the table when the object is collected.
Ok … thanks. So, I did some Googling, and both Ted Patrick and David Coletta have much better information on the subject.
The basic idea behind strong references and addEventListener is this: when you call addEventListener and you pass it the function that you want to be called when the event fires, the event dispatcher is actually grabbing and holding a reference to that class and function it contains. As we all know, an object won’t be garbage collected until every reference to it doesn’t exist anymore. This insures that the event dispatcher always has a reference to the function that needs to be called. However, if you’re not careful, this can be a breeding ground for nasty memory leaks.
So, here’s my short list of do’s and don’ts related to strong vs weak references and event listeners:
- Always use a weak reference when adding an event listener to a class that’s a singleton, or any object that you expect to live throughout the life of your application. Remember, this applies to most of Flex’s manager classes, including the SystemManager, and any event dispatching classes that your singleton’s contain.
- Always use weak references when adding Cairngorm commands. The event dispatcher in Cairngorm is a singleton, and will always keep references to those command classes.
- According to Ted, weak references are 10x slower. (True when referring to member access, but not event listening.)
- It’s safe to use strong references when you’re adding an event listener to itself (i.e. the instance dispatching the event).
- You should use a weak reference when adding an event listener to the Timer class.
- Lastly, always make sure to clean up and remove any event listeners your classes are using when they’re no longer needed. I follow .NET’s approach, and I have an interface called IDisposable. This interface contains a single method called
dispose(), and inside I remove any event listeners that the class might be using. Additionally, you could clean-up any BitmapData objects you might be using, among other things. Most, if not all, of my classes that I write implement this interface, and I just make sure to calldispose()when I know the object is no longer needed.
If there’s anything else to look out for, I’d love to hear them. It’s always good to be proactive when it comes to memory leaks.
Embedding Cayri on Your Website
Lately I’ve been getting emails from people asking me if they can embed Cayri on their virtual airline’s website. So I think it’s about time to post a small tutorial on how to do this. The simplest way to do it is by using an IFRAME tag with HTML. Just stick the following code in any HTML page, and Cayri will show up on your website:
1 | <iframe src="http://www.cayri.org/?network=vatsim" frameborder="0" height="500" width="700"></iframe> |
If you would like to show IVAO traffic instead of VATSIM traffic, you’ll need to replace ?network=vatsim with ?network=ivao in the src attribute.
About Dan
Categories
- ActionScript (1)
- AIR (5)
- Cayri (7)
- Design Patterns (4)
- Flex (8)
- Mapping (1)
- Mesh (1)
- Mixbook (1)
Recent Posts
- Mesh – A Persistence Framework For Flex
- Making ActionScript Look Beautiful: Increasing Readability and Loosing Verboseness
- Help Dan find a Flex Engineer and get a free paid trip for 2 to Hawaii
- What’s Happening With Cayri?
- Saving Flex’s Application State to a Database
- Loading Modules/Runtime CSS into an AIR App
- CollectionEventKind.RESET, WTF Does That Mean?
- When to Use Weak References
- Embedding Cayri on Your Website
- The State Pattern in Flex 2
- @zeeyang just get an iPhone already :)
- @neiltyson I'd love to see you speak this week, but damn.. $270!! Your talk costs 3x more than a rock concert.
- @almeida_miguel sorry, but Mixbook doesn't offer licensing of its software at the moment.
- @usbank -1 for telemarketing your own customers.
- I might go to The Black Keys @ San Jose City College. http://t.co/d81dAPfu




