简体   繁体   中英

C# Should I manually remove the event handler I declared?

Okay, make an example here:

  1. I have UserControl A, UserControl B, UserControl C and one Windows Form.
  2. This Windows Form is only started with UserControl A.
  3. UserControl C has [Next] and [Back] buttons.
  4. Say, UserControl A is declared with an event handler. One of function in UserControl A will actually raise the event call to execute one function at UserControl C.
  5. So, at UserControl C, I have to add with

"UserControlA.OneFunction += this.UserControlC_Function;"

  1. If I click Next button at UserControl C, it will dispose the UserControl A and add new UserControl B to the Windows Form. But I never remove this event handler manually.

One of the function in UserControl A is the caller (where event is declared).
One of the function in UserControl C is the listener.

So, these are my questions:

  • Should I manually remove the handler before UserControl A disposed?
  • Will this User Control A dispose automatically remove the handler that declared previously?
  • Should I add this somewhere?

"UserControlA.OneFunction -= this.UserControlC_Function;"

  1. By convention, we don't. And since no event should be invoked after disposal, there is no need to do so unless the control in question is behaving weirdly.
  2. No. At least there isn't such code as seen from reflector.

在这种情况下,您不需要删除处理程序,因为表单外部的代码都不会引用表单及其按钮,因此整个对象图将被垃圾回收。

The answer to this post does a really good job explaining when you need to manually remove an event handler and when it is not necessary.

Do I need to remove event subscriptions from objects before they are orphaned?

If the form is released (assuming no other objects has a reference to the objects in question) there's little risk of not removing the event handler, however it's a good idea always to remove the event handler before the object listening can no longer be reach (ie all variables referencing the object i sout of scope) not doing so can create a memory leak.

This is not the case in your situation (if I get what you are describing, code would make it more clear) The problem would be if you attach a delegate referencing object C to an event on object A and then looses access to C (eg assigning a new value to the variable). C would then hang around until A is garbage collected

If the memory lifetime of an event publisher is not limited relative to the useful lifetime of an event subscriber, failure to unsubscribe an event will likely cause a memory leak. Were it not for the unfortunate hassle of doing so, there wouldn't be any reason for an event subscriber that was being disposed not to unsubscribe from all events, and for an event publisher that was being disposed not to nullify all event subscriptions. Since neither C# nor VB provides any convenient means of doing those things, however, one has to balance the hassle of proper subscription handling with the fact that in many situations one can get away skimping on it.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM