Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

If my software has two object instances, one of which is subscribed to the events of the other. Do I need to unsubscribe them from one another before they are orphaned for them to be cleaned up by the garbage collector? Or is there any other reason why I should clear the event relationships? What if the subscribed to object is orphaned but the subscriber is not, or vise versa?

like image 761
Eric Anastas Avatar asked Jul 02 '09 20:07

Eric Anastas


People also ask

When to unsubscribe from event C#?

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 add method to event in C#?

Use the addition assignment operator ( += ) to attach an event handler to the event. In the following example, assume that an object named publisher has an event named RaiseCustomEvent . Note that the subscriber class needs a reference to the publisher class in order to subscribe to its events.


1 Answers

Yes you do. The event publishers are holding references to the objects, and would prevent them from being garbage collected.

Let's look at an example to see what happens. We have two classes; one exposes an event, the other consumes it:

class ClassA {     public event EventHandler Test;     ~ClassA()     {         Console.WriteLine("A being collected");     } } class ClassB {     public ClassB(ClassA instance)     {         instance.Test += new EventHandler(instance_Test);     }      ~ClassB()     {         Console.WriteLine("B being collected");     }      void instance_Test(object sender, EventArgs e)     {         // this space is intentionally left blank     } } 

Note how ClassB does not store a reference to the ClassA instance; it merely hooks up an event handler.

Now, let's see how the objects are collected. Scenario 1:

ClassB temp = new ClassB(new ClassA()); Console.WriteLine("Collect 1"); GC.Collect(); Console.ReadKey(); temp = null; Console.WriteLine("Collect 2"); GC.Collect(); Console.ReadKey(); 

We create a ClassB instance and hold a reference to it through the temp variable. It gets passed a new instance of ClassA, where we do not store a reference to it anywhere, so it goes out of scope immediately after the ClassB constructor is done. We have the garbage collector run once when ClassA has gone out of scope, and once when ClassB as gone out of scope. The output:

Collect 1 A being collected Collect 2 B being collected 

Scenario 2:

ClassA temp = new ClassA(); ClassB temp2 = new ClassB(temp); temp2 = null; Console.WriteLine("Collect 1"); GC.Collect(); Console.ReadKey(); temp = null; Console.WriteLine("Collect 2"); GC.Collect(); Console.ReadKey(); 

A new instance of ClassA is created and a reference to it is stored in the temp variable. Then a new instance of ClassB is created, getting the ClassA instance in temp passed to it, and we store a reference to it in temp2. Then we set temp2 to null, making the ClassB instance going out of scope. As before, we have the garbage collector run after each instance has gone out of scope. The output:

Collect 1 Collect 2 B being collected A being collected 

So, to conclude; if the instance that exposes an event goes out of scope, it becomes available for garbage collection, regardless of whether there are event handlers hooked up or not. If an instance that has an event handler hooked up to an event in another instance, it will not be available for garbage collection until either the event handler is detached, or the instance to which the event handler is attached becomes available for garbage collection.

like image 198
Fredrik Mörk Avatar answered Oct 29 '22 11:10

Fredrik Mörk