views:

251

answers:

3

I know that weak references are a good candidate for memoizing potentially large sets of data, and Wikipedia's article on weak references only lists "keeping track of the current variables being referenced in the application" and the statement "Another use of weak references is in writing a cache".

What are some other situations (more specific than just "caching results") where the use of weak references is A Good IdeaTM?

+1  A: 

In Flex, use weak references to avoid memory leaks.

If an event handler is a member of a short-lived instance object, passing the handler as a strong reference to an object which will live longer may keep the short-lived instance alive unnecessarily.

Chadwick
A: 

I use weak references for a few things...

I like to create "Weak Events" in .Net to avoid observables from keeping observers alive too long.

I have also used weak events to detect memory leaks.

Brian Genisio
A: 

In Python, the garbage collector uses reference counting to decide when to "destroy" or otherwise deallocate an object. A normal circular reference can result in objects never being garbage collected because their reference counts respectively stay at 1 or higher; but using weak references will allow both/all objects to be properly cleaned up when they go out of scope.

Mark Rushakoff