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?
Go to the objects tab, view the Document object (don't click on edit) and scroll down to Event Handlers. Select the one to delete and press delete.
In programming, an event handler is a callback routine that operates asynchronously once an event takes place. It dictates the action that follows the event. The programmer writes a code for this action to take place. An event is an action that takes place when a user interacts with a program.
The EventHandler delegate is a predefined delegate that specifically represents an event handler method for an event that does not generate data. If your event does generate data, you must use the generic EventHandler<TEventArgs> delegate class.
An event handler, in C#, is a method that contains the code that gets executed in response to a specific event that occurs in an application. Event handlers are used in graphical user interface (GUI) applications to handle events such as button clicks and menu selections, raised by controls in the user interface.
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
Many people seem to think that it's only important to unsubscribe from events if the publisher is going to outlive the subscriber. I dislike that approach. An event subscriber which does not detach itself from the publisher creates some a nasty dependencies on the behavior of entities outside the publisher and subscriber. If a reference to the publisher is held longer than expected, that will keep the subscriber alive, along with any objects to which the subscriber holds a reference. If a large mass of abandoned objects are interconnected by event handlers, but no live reference exists to any of them, all the objects can be swept up by the garbage collector. If, however, someone somewhere unexpectedly keeps a reference to one of the objects, that may prevent any of them from being garbage-collected.
IMHO, it's much better to be proactive in removing event handlers than to abandon them and hope that everything gets cleaned up. Unless one can be certain that no unexpected references to the publisher can exist, such an approach is likely to 'mostly' work, but cause occasional memory leaks.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With