hivemind-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Luke Blanshard <>
Subject Re: Potential memory leak with Threaded model and Events listeners
Date Sat, 04 Dec 2004 13:12:37 GMT

The problem with holding listeners weakly is that people commonly create 
listener objects on the fly and then retain no other references to them 
once they've added them.  So these listeners are thrown away on the next 
GC cycle, and they stop working.

One solution to this is to have a tagging interface (we call ours 
WeakListener) that tells the event source that it should hold the 
listener weakly.  All listeners that don't implement this interface are 
held strongly.  This way, you as the implementer of the listener are 
telling the event source whether to hold the listener weakly (meaning 
you better have a strong reference to it) or strongly (meaning you don't 
have to).  As you say, it's pretty easy to write a listeners collection 
that honors this WeakListener interface and throws away the expired 
listeners when you ask for the current list of listeners.  (We don't use 
an iterator, but rather a method that returns an array of listeners, for 
thread safety reasons.)


Jean-Francois Poilpret wrote:

>To Howard:
>During the night (French people say that the "night gives good advice";-)),
>I thought of a quite easy workaround, that can be done at the event
>supplying service level: just use a weak reference to the listeners (instead
>of a strong one), I think that should do the trick.
>Maybe it would even be possible to write a general "WeakEventListeners"
>class to manage all that for the event supplier:
>- when addListener, is called generate a weak reference to the added
>listener and store it in the list
>- create a specific Iterator for WeakEventListeners that would skip the refs
>that have become weak-reachable (and remove these from its list at the same

To unsubscribe, e-mail:
For additional commands, e-mail:

View raw message