34

Is it a good practice to implement event handling through WeakReference if that event is the only thing holding the reference and that we would need the object to be garbage collected?

As an argument to this:

Folks say that if you subscribe to something it’s your responsibility to unsubscribe and you should do it.

MatthewMartin
  • 32,326
  • 33
  • 105
  • 164
Cherian
  • 19,107
  • 12
  • 55
  • 69
  • The question becomes: What are the chances that that event gets fired by the time the object is garbage collected? Why use a WeakReference in the first place? – Jon Limjap Oct 09 '08 at 04:27
  • @Jon Limjap without a weakreference, isn't that chance zero because the event keeps a track of the object and hence it will not be collected? – fostandy Apr 21 '11 at 04:55
  • 2
    @fostandy: No, the publisher of an event will not be kept alive by subscribers, it only works the other way around. To allow subscribers to be GC'ed without having to unsubscribe first, one should use WeakReference. See: http://stackoverflow.com/a/298276/134761 – angularsen Jan 03 '12 at 06:37

4 Answers4

15

It is good to get in the habit of unsubscribing from events when you can, but sometimes there isn't an obvious "cleanup" method where it can be done. We recently posted a blog article on this subject; it includes methods that make it easy to subscribe to an event with a WeakReference.

Shayan Toqraee
  • 735
  • 8
  • 18
Ed Ball
  • 1,979
  • 2
  • 14
  • 13
12

Weak delegate pattern is something that should be there in CLR. Normal events exhibit "notify me while you are alive" semantics, while often we need "notify me while I'm alive". Just having delegate on WeakReference is wrong, because delegate is an object too and even when recepient is still alive and have incoming references, delegate itself is only being referenced by said WeakReference and will be collected instantly. See this old post for an example of implementation.

codersl
  • 2,222
  • 4
  • 30
  • 33
Ilya Ryzhenkov
  • 11,782
  • 1
  • 40
  • 50
7

Weak references in their own right, don't solve the problem as the delegate holds the reference. In the Composite Application Library which ships with Prism (www.microsoft.com/compositewpf) there is a WeakDelegate class that you could pull from the source. The WeakDelegate basically ues reflection and creates a delegate only for a moment in time and then releases it, thereby no holding any pointers. Within CAL it is used by the EventAggregator class, but you are free to rip it out for your own usage as it is under MS-PL.

Glenn Block
  • 8,463
  • 1
  • 32
  • 34
-2

While what you suggest solves one set of problems (event reference management and memory leak prevention), it is likely to open up a new set of problems.

One problem I can see is during event handling process if the source object is garbage collected (as it was only held with a weak reference), any code that access the source object will result in null reference exception. You can argue that the event handler should either not access the source object or it must have a strong reference, but it can be argued that this could be a worse problem than the one you are trying to solve in the first place.

Samuel Kim
  • 3,723
  • 2
  • 23
  • 18