views:

669

answers:

1

If I have an application with only a few event handlers registered (and the objects using the events are not disposed until the application is closed), do I really need to worry about unregistering those handlers? The only good reason I could see is that there might be a little extra overhead if events are being fired that you dont necessarly care about (i.e you have multiple handlers registered to one event). Is there any other good reason to? Anyone run into major issues because they didnt unregister events?

+14  A: 

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...

Marc Gravell
This is especially bad if B is resource heavy...
Nader Shirazie
Great insight Marc. handlers for static events are really notorious if not taken care while disposing the subscriber.
TheVillageIdiot
I know this is a old post and I do not know if it will ever get read but if B lives a lot longer than A, will B keep A from being garbage collected?
Scott Chamberlain
@Scott. No - `B` has no reference to `A` through the event; `A` will be collected as normal.
Marc Gravell