IDisposable for dummies #2 – A guide about ‘how to implement it’

IDisposable - How ?In my previous post, I classified the different memory resources available in the .NET CLR and I explained the role of the IDisposable interface as well as the use of the Finalize() method.

In this post, I am going to talk more about the implementation details of IDisposable and Finalize().

For reference, I have split this matter into two posts:

(more…)

IDisposable for dummies #1 – Why? What?

(IDisposable - What ? Why?It took me more than a year to finish writing this article – a “hot topic” and I wanted it to be clear, simple and right. Also, I was busy and did not want to publish anything “unfinished”. As usual, your feedback and comments are more than welcome. Thank you in advance)

Recently (February 2011), I had to review some .NET code and came across of some wrong implementations of IDisposable.

After discussing with the developers, there were many reasons for what I found, ranging from:

  • Not knowing the difference between a “CLR memory resource”, a “managed resource” and an “unmanaged resource”;
  • Not understanding “how and when”  resources are “released”;
  • Not knowing when to override the “Finalize()” method and what should be released by a finalizer?

(more…)