Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Do I have to unsubscribe from button events after using it in c#?

Tags:

c#

I am using windows forms.

My C# application contains 100 user controls. I show/hide one of those 100 user controls at a time when I need to and hide the rest. Each one of those user controls has 30 buttons and I subscribe to button event as following in the constructor:

public UserControl1()
{
    InitializeComponent();

    button1.Click += new EventHandler(MyButtonClick);
    button2.Click += new EventHandler(MyButtonClick);
        .

        .
    button30.Click += new EventHandler(MyButtonClick);
}

void MyButtonClick(object sender, EventArgs e)
{
    // do something
}

So when I run the Application all the 100 User controls subscribe to the 30 buttons event and some of the user controls subscribe to the event but they are never used during the use of the application.

I read something about unsubscribing events here and Here but some answers say you should unsubscribe because it cause memory leak and some say you don't have to, therefore the answer is still not clear.

My question is do I have to unsubscribe from button events after using it for example: when I show/hide a user control. If yes, how can I subscribe from button event when a user control is shown and unsubscribe when it is not shown.

like image 790
Kate Avatar asked Aug 23 '16 12:08

Kate


People also ask

Do you need to unsubscribe from events?

In order to prevent resource leaks, you should unsubscribe from events before you dispose of a subscriber object. Until you unsubscribe from an event, the multicast delegate that underlies the event in the publishing object has a reference to the delegate that encapsulates the subscriber's event handler.

How to handle events in C#?

To define an event, you use the C# event or the Visual Basic Event keyword in the signature of your event class, and specify the type of delegate for the event.


3 Answers

Why unsubscribe?

It comes down to the lifespan of the subscriber (UserControl) compared to the subscribe (Button). The subscribe hold a reference to the subscriber. So if the subscribe have longer lifespan then the subscriber it will memory leak the subscriber.

So in your case you should ask if the Buttons will persist longer then the UserControl. If the Buttons have shorter on equal lifespan then no need to unsubscribe. Otherwise you will memory leak the UserControl.

In your case I guess that you don't need to unsubscribe.

like image 53
R.Rusev Avatar answered Oct 13 '22 01:10

R.Rusev


If I got your question right - each control subscribing only on IT'S OWN children (30 buttons). The case when forgetting to unsubscribe is bad idea is when publisher (button) will live longer than subscriber (user control). Why? Because publisher will store link to subscriber and will prevent that subscriber to be disposed by Garbage Collector. In your case button will never live longer than it's parent - user control, so you don't need to unsubscribe.

like image 23
Vladyslav Kushnir Avatar answered Oct 13 '22 02:10

Vladyslav Kushnir


In your case there is no reason to worry. The two examples that you linked to have a different 'kind' of event, like this: if object A changes, then method X should be called, so X can act on or process the change in object A. For example A = inventory of a product; if the count of A goes to zero then it can no longer can be sold, or new items must be ordered, etc.

When responding to changed data, then the need to call X could cease to exist at some point in time, and then it may be beneficial to unregister the event, especially if there is no other way to break or disable the link.

However in a User Interface scenario such as yours, the need to call X usually stays present. If needed it can be controlled in another way, e.g. by disabling the button or by hiding it. There is really nothing expensive about having a link between a button and a method. The only reason to break the link would be 'I no longer want X to be called EVER if the button is clicked'.

like image 29
Peter B Avatar answered Oct 13 '22 02:10

Peter B