Is it bad to not unregister event handlers?

If you have A publishing an event, and B subscribing to an event (the handler), then it is only a problem not to unsubscribe if A is going to live a lot longer than B. Basically, the event subscription means that A can still see B, so would prevent it from being garbage collected, and would still fire events on it even if you’ve forgotten about it (and perhaps Disposed() it).

For example, this is a problem if A is a static event, and your app runs for a while after B dies… ButB will live as long as A , thus B will not be garbage collected.

It is important to note, one might ask the following:

if B lives a lot longer than A, will B keep A from being garbage collected?

And the answer to that is “no”. B has no reference to A through the event; A will be collected as normal

Leave a Comment