1

I stumbled over this code snippet at How to remove all event handlers from a control :

void OnFormClosing(object sender, FormClosingEventArgs e)
{
    foreach(Delegate d in FindClicked.GetInvocationList())
    {
        FindClicked -= (FindClickedHandler)d;
    }
}

My question is: assuming that the form is about to be disposed (after being closed), would the snippet be required or would FindClicked and all references to event handler delegates simply go down with the form when it is GC´ed (which the referenced event handlers shouldn't cause any problems with)?

mike
  • 1,627
  • 1
  • 14
  • 37

1 Answers1

2

Documentation suggests that you should remove all event handlers when disposing:

In order to prevent resource leaks, you should unsubscribe from events before you dispose of a subscriber object.

You can also check out this other answer, which might be useful: Does `Control.Dispose()` remove all event registrations?

Thomas English
  • 231
  • 1
  • 8
  • thanks for the ultra-fast response. why did you edit the link? https://stackoverflow.com/questions/25934267/does-control-dispose-remove-all-event-registrations helped me a whole lot more! – mike Mar 27 '18 at 22:45
  • @mike You're welcome, I edited the link because it was pointing to the wrong source for my quote. The Microsoft documentation seemed more authoritative. I've re-added the link to the other question for anyone else who may find that post useful. – Thomas English Mar 27 '18 at 22:48