1

I need bit of advise on best practice for this type of scenario. I searched but dont find any satisfying answer.

We use a 3rd party (.net) DLL in our winforms project. It raises some events and we subscribe to. Question is , do i need to explicitly unsubscribe these events in my class ?

BTW we both use .net framework 4. Thanks for the advise.

some sample code ...

public class MyClientCode: IDisposable
{
   private readonly 3rdParty.TheirClass _theirClass;
   public MyClientCode()
  {
     _theirClass = new _theirClass()
     _theirClass.ReadData += ReadDataEvent;
  }

  public void SomeOtherMethod()
  {
           //some other code
  }

  public void ReadDataEvent()
  {
    //some code
  }

  public void Dispose()
  {
    _theirClass.ReadData -= ReadDataEvent;
  }
}

and in the button click event, i do ...

 MyClientCode code = new MyClientCode();
 code.SomeOtherMethod();
John Saunders
  • 160,644
  • 26
  • 247
  • 397
rkp
  • 11
  • 4

1 Answers1

4

If you don't unsubscribe, the object that subscribed to the event will not be garbage collected (it will be kept in memory). This can create memory leaks and lead to excessive memory usage.

However, if the event has a shorter or same lifetime as the class that contains it, in your case, the memory will be collected properly. If you have another object reference a non-private event, then you will run into issues.

See MSDN:

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. As long as the publishing object holds that reference, your subscriber object will not be garbage collected.

Note that you don't need to unsubscribe if you are exiting your application, only in cases where it shouldn't be held in memory. (For example, when you close a window in your application, you should unsubscribe from any events, or else the window will still be held in memory.) If the containing object is destroyed manually, the events will also be destroyed.

Cyral
  • 13,999
  • 6
  • 50
  • 90
  • Thanks for the reply. is there anyway (automatic way) to call this dispose method if one of us forgets to call dispose method? – rkp Jun 18 '15 at 02:01
  • There is no automatic way, see [this](http://stackoverflow.com/a/14193252/1218281) question which shows how to use `using` blocks around `IDisposable`s. – Cyral Jun 18 '15 at 02:04
  • Note that Cyral didn't mention Dispose. – John Saunders Jun 18 '15 at 02:04
  • @JohnSaunders In the OP he is using dispose to unsubscribe from the event. – Cyral Jun 18 '15 at 02:05
  • Cyral, i understand that i could use "using" but we've several developers who may use this code and want to make sure dispose the object even if the forget. – rkp Jun 18 '15 at 02:06
  • AFAIK, there is no need to unsubscribe from an event if you hold the only reference to the publishing object. Once your class is eligible for collection, the publishing object will also be eligible. It doesn't matter if it holds a reference to your object, if it itself is not reachable. – Blorgbeard Jun 18 '15 at 02:07
  • @Blorgbeard I think your right, I assumed external references. – Cyral Jun 18 '15 at 02:10
  • @cyral, hence my question in the comments on the OP :) – Blorgbeard Jun 18 '15 at 02:11
  • @Blorgbeard if i dont explicitly call dispose (ie. unsubscribe events) i am getting errors from the 3rd party dll. probably GC not collecting fast enough. – rkp Jun 18 '15 at 02:22
  • its a windows service this DLL provides and its throwing errors connecting the service if i try to use my class 2nd time. BTW my calling code is in a button click event. – rkp Jun 18 '15 at 02:26
  • @John - You must explicitly call `.Dispose()` or use `using`. The GC does **NOT** call `.Dispose()` for you. – Enigmativity Jun 18 '15 at 02:47
  • OK, so if your private instance is actually connected to a windows service, then the thing you're (really) subscribing to lives longer than your class - so you *do* need to unsubscribe. – Blorgbeard Jun 18 '15 at 03:21
  • @Cyral: interesting. I searched for "Dispos" but didn't see it. – John Saunders Jun 18 '15 at 03:24