I'm trying to understand how a Control
events are unsubscribed. Suppose I have a textbox and I have subscribed the TextChanged
event using the WinForms designer.
Is the TextChanged
event automatically unsubscribed in the Textbox
destructor, or must I explicitly unsunscribe to avoid memory leaks?
public void InitializeComponents()
{
...
this.emailTextBox.TextChanged += emailTextBox_TextChanged;
...
}
public override void Dispose()
{
if( disposing )
{
// DO I REALLY NEED THIS LINE?
this.emailTextBox.TextChanged -= emailTextBox_TextChanged;
if(components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
}
See Question&Answers more detail:os