Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Must I unsubscribe all event handlers?

Tags:

c#

.net

winforms

From the Designer in VS let's say you double click on a button and it generates this Click event handler.

the subscription code is in the designer.cs.

I was wondering, in the dispose the Form MUST I unsubcribe the event ?

Also, all control that are in the form will it be disposed when the forms disposes? it actually call dispose on each control recursively?

like image 577
pdiddy Avatar asked Aug 27 '09 14:08

pdiddy


People also ask

What are event handlers used for?

Event handlers can be used to handle and verify user input, user actions, and browser actions: Things that should be done every time a page loads. Things that should be done when the page is closed. Action that should be performed when a user clicks a button.

How many event handlers are there?

Only one event handler can be assigned for every event in an element. If needed the handler can be replaced by assigning another function to the same property. Below we show how to set a simple greet() function for the click event using the onclick property.

What are events handled by event handler?

To handle a click event on an HTML element, we'll use an event handler property. An event handler property is a built-in property of an object (a Web API) that represents an event. Our window , document , and HTML elements all have built-in event handler properties.


3 Answers

You don't need to unhook the event on Dispose if you are hooking your own event.

You only need to worry about it if you are hooking an event in another object. The reason for this is that event hooks keep a reference alive to the subscriber. If you fail to unhook, then you won't get garbage collected as long as the observable is still alive.

When you hook your own event, you have a reference to yourself, which is circular, therefore you don't need to worry about it.

I have come to support more loosely coupled event patterns for this reason. This is the #1 place for memory leaks in .Net. I prefer the Event Aggregator pattern (with weak events).

like image 120
Brian Genisio Avatar answered Nov 12 '22 00:11

Brian Genisio


On the question "will it actually call dispose on each control recursively?", the answer is yes.

A simple test can be done by putting a breakpoint in the Dispose method of a control.

public partial class Form1 : Form
{
    public Form1()
    {
        InitializeComponent();

        this.Controls.Add(new SuperButton());
    }
}

public class SuperButton : Button
{
    protected override void Dispose(bool disposing)
    {
        //Place breakpoint on the line below
        base.Dispose(disposing); 
    }
}
like image 25
Pierre-Alain Vigeant Avatar answered Nov 12 '22 00:11

Pierre-Alain Vigeant


As long as the event handler code is the form itself, then you wouldn't need to unsubscribe the events - as there wouldn't be a dangling event handler to the controls as the form itself would be destructed

like image 27
saret Avatar answered Nov 12 '22 02:11

saret