What's the point of overriding Dispose(bool disposing) in .NET? - c#

If I write a class in C# that implements IDisposable, why isn't is sufficient for me to simply implement
public void Dispose(){ ... }
to handle freeing any unmanaged resources?
Is
protected virtual void Dispose(bool disposing){ ... }
always necessary, sometimes necessary, or something else altogether?

The full pattern including a finalizer, introduction of a new virtual method and "sealing" of the original dispose method is very general purpose, covering all bases.
Unless you have direct handles on unmanaged resources (which should be almost never) you don't need a finalizer.
If you seal your class (and my views on sealing classes wherever possible are probably well known by now - design for inheritance or prohibit it) there's no point in introducing a virtual method.
I can't remember the last time I implemented IDisposable in a "complicated" way vs doing it in the most obvious way, e.g.
public void Dispose()
{
somethingElse.Dispose();
}
One thing to note is that if you're going for really robust code, you should make sure that you don't try to do anything after you've been disposed, and throw ObjectDisposedException where appropriate. That's good advice for class libraries which will be used by developers all over the world, but it's a lot of work for very little gain if this is just going to be a class used within your own workspace.

It's not strictly necessary. It is part of the recommended Disposable pattern. If you haven't read the Framework Design Guidelines section on this (9.3 in the first edition, don't have the second edition handy sorry) then you should. Try this link.
It's useful for distinguishing between disposable cleanup and finalizable garbage-collection-is-trashing-me.
You don't have to do it that way but you should read up on it and understand why this is recommended before discounting it as unnecessary.

There's a bit of bias in the MSFT docs about the disposable pattern. There are two reasons you should implement IDisposable:
You've got fields of a type that implements IDisposable
You've got a finalizer.
Case 1 is pretty common in most code. Case 2 is pretty common in code that Microsoft writes, they were the ones that wrote the managed wrappers around the unmanaged resources, the ones that need finalization. But should be very uncommon in your code. After all, you've got all those nice .NET classes to do the dirty work for you. You just have to call their Dispose() methods.
Only case 2 requires the disposable pattern. Microsoft needs to use it a lot. You'll just need the simple Dispose() most of the time.

In addition to the other great answers, you may want to check these articles:
Implementing IDisposable and the Dispose Pattern Properly
IDisposable: What Your Mother Never Told You About Resource Deallocation (The Disposable Design Principle)

The additional method with the bool disposing came out of a framework design guideline somewhere. It is simply a pattern to allow your class to have the dispose method be able to be called multiple times without throwing an exception. It isn't absolutely needed. Technically you could do it in the dispose method.

Just to expand on what others have said: it's not just that you don't need the 'complex dispose', it's that you actually don't want it, for performance reasons.
If you go the 'complex dispose' route, and implement a finalizer, and then forget to explicitly dispose your object, your object (and anything it references) will survive an extra GC generation before it's really disposed (since it has to hang around one more time for the CLR to call the finalizer). This just causes more memory pressure that you don't need. Additionally, calling the finalizer on a whole heap of objects has a non-trivial cost.
So avoid, unless you (or your derived types) have unmanaged resources.
Oh, and while we're in the area: methods on your class which handle events from others must be 'safe' in the face of being invoked after your class has been disposed. Simplest is to just perform a no-op if the class is disposed. See http://blogs.msdn.com/ericlippert/archive/2009/04/29/events-and-races.aspx

One thing that it gives you is the ability to do work in Dispose() unrelated to finalization, and still clean up unmanaged resources.
Doing anything to a managed object other than 'yourself' in a finalizer is extremely... unpredictable. Most of this is due to the fact that your finalizers will be called in stage 2 shutdown of your AppDomain in a non-deterministic manner - so when your finalizer is called, it is extremely likely that objects that you still have references to have already been finalized.
Dispatching both the Dispose and finalizer calls to the same method allows you to share your shutdown code, while the boolean parameter allows you to skip the managed cleanup if you have any.
Also, the virtual-ness of the method provides an easy way for inheritors to add their own cleanup code, with less of a risk of inadvertently not calling yours.

If a class implements IDisposable.Dispose() and a derived class needs to add additional logic, that class must expose some kind of Dispose method that the derived class can chain to. Since some classes may implement IDisposable.Dispose() without having a public Dispose() method, it's useful to have a virtual method which will be protected in all implementations of IDisposable regardless of whether they have a public Dispose method or not. In most cases, the bool argument isn't really meaningful but should be thought of as a dummy argument to make the protected virtual Dispose(bool) have a different signature from the may-be-or-maybe-not-public Dispose().
Classes which doesn't use a protected virtual Dispose(bool) will require derived classes to handle their cleanup logic in a fashion which differs from the convention. Some languages like C++/CLI which are only equipped to extend IDisposable implementations which follow that convention may be unable to derive classes from non-standard implementations.

Related

What C# class objects acquire unmanaged resources? Is there a list?

I was working on serializing and deserializing a class object using XML when I came across this blog post that shows how to do it on Windows Phone 7 using the isolated storage area. Windows Phone 7 is the platform I am developing for:
In this example, the only object he explicitly calls Dispose() on is the TextReader object. I looked up the TextReader object on MSDN and found that the documentation said this:
Releases the unmanaged resources used by the TextReader and optionally releases the managed resources.
So I assume the reason he does this is to release immediately the unmanaged resources acquired by the TextReader object. It would not have occurred to me to do this if it weren't for his blog post. Obviously I don't want to start calling Dispose() on every object in sight, so what is a good rule of thumb for at least investigating when a particular object should have Dispose() called on it or not? Are there some guidelines for this or a list somewhere, at least of the popular .NET objects that require this special handling?
Obviously I don't want to start calling Dispose() on every object in
Wrong.
In general, any object that implements IDisposable should be disposed as soon as you're finished with it, typically using the using statement.
Most objects that do not have unmanaged resources do not implement IDisposable (and do not have Dispose() methods), so you have nothing to worry about.
The only exceptions are base classes that implement IDisposable in case some derived implementations have something to dispose (eg, IEnumerator, Component, or TextReader).
However, it is not always obvious which concrete implementations need to be disposed (and it may change at any time), so you should always dispose them anyway.
Obviously I don't want to start calling Dispose() on every object in sight, so what is a good rule of thumb for at least investigating when a particular object should have Dispose() called on it or not?
This is not a problem. The compiler won't let you call Dispose() on an object that doesn't implement it.
And you should be calling Dispose() for every object that does implement it (which it will do via the IDisposable interface). That is the guideline you should be following. In fact, that's what it means when an object implements IDisposable: that it has unmanaged resources that need to be released.
It becomes much less of a chore if you'll simply wrap the creation and use of the objects in a using statement, e.g.:
using (DisposableObject obj = new DisposableObject(...))
{
obj.DoWork();
} // obj.Dispose() is automatically called here, even if an exception is thrown
Actually you do have to dispose of objects which implement IDisposable.
The standard way of doing that as opposed to directly calling the Dispose() is:
using(AnyIDisposable obj = ...)
{
// work with obj here
}
//The Dispose() method is already called here
Please correct me if i'm wrong.
As far a i read/understood, all clases of the .NET Framework are managed (to the view of the programmer, although underderlaying they might use unmanaged code), so theoretically you dont need to call Dispose() or using, because the gc will take care. But sometimes it's very recommended to use them, see IDisposable Interface and
Which managed classes in .NET Framework allocate (or use) unmanaged memory? and http://blogs.msdn.com/b/kimhamil/archive/2008/11/05/when-to-call-dispose.aspx
EDIT: (you are right noob) For clarification i'll add Nayan answer from IDisposable Interface
It recommended to call dispose or using, when:
1.You class has many objects and there are of lots of cross references. Even though its all managed, GC may not be able to reclaim
the memory due to alive references. You get a chance (other than
writing a finalizer) to untangle the references and break up the links
the way you attached them. Hence, you are helping the GC to reclaim
the memory.
2.You have some streams open which are alive till the object of the class dies. Even though such implementations of files/network etc are
managed, they go deep down to handles in Win32 mode. Hence, you get a
chance to write a Dispose method where you can close the streams. The
same is true for GDI objects, and some more.
3.You are writing a class which uses unmanaged resources, and you want to ship your assembly to third parties. You better use disposable
pattern to make sure you are able to free the handles to avoid the
leakage.
4.Your class implements lots of event handlers and hooks them up to events. The objects of classes which
expose the events, like Form etc., will not be freed up by GC since
the implementations local to your class (maybe) are still hooked into
those events. You can unhook those event handlers in Dispose; again
helping GC.

asp.net best way to clean up unmanaged resources

I am a little confused about memory clean up in an ASP.NET application. I had defined several destructors--I know this isn't the new .NET way of doing things, but I am a creature of habit and I always did it this way in c++-- that were working wonderfully in just about every scenario. However, I have noticed that they are sometimes not called in my ASP.NET applications.
I am thinking about implementing IDisposable, but I am under the impression that IDisposable is for other users of your code, and I am not sure that ASP.NET would call Dispose when it is finished with the object. Could someone clarify on this?
What is the best, and by best I mean that it will always work-- way to clean up my unmanaged memory?
Edit
This seems to indicate that if the class containing potential unmanaged memory is a member of an encapsulating class, then the destructor is the best strategy. This certainly makes sense to me since I could hardly put a try or a using around a class member. Even then however, that brings me back to my question, it sometimes never gets called in my ASP.NET app.
All classes which handle unmanaged resources should implement the IDisposable interface.
For a little more info, there are two issues with the garbage collector. First, you have no idea when it's going to run. Second, it has zero knowledge of unmanaged resources.. That's why they are called unmanaged.
Therefore it's up to the calling code to properly dispose of objects that utilize unmanaged resources. The best way to do this is to implement the above interface and either wrap the object in a using ( ) { } statement or, at the least, a try .. finally. I generally prefer the using statement.
Also, by implementing IDisposable you are signaling to other developers that this class deals with unmanaged resources so they can take the appropriate steps to ensure things are called correctly.
When working with managed resources, you don't need to implement IDisposable or a destructor. All you have to do for "cleanup" is set all top-level ("rooted") references to null (statics are normally considered to be top-level), and the garbage collector will take care of the rest.
Destructors as such are primarily useful with unmanaged resources in cases where callers either forget to call Dispose, or where such a call isn't possible. However, the runtime doesn't guarantee that destructors will ever be called; only that they will be called before the memory associated with the object is finally freed. You don't have to implement IDisposable; it's just a convention. It's perfectly reasonable to have a Close() or Cleanup() method that releases unmanaged resources.

C#:What should be the content of the Dispose method when implementing IDisposable interface

I created a class which implements IDisposable interface and VisualStudio IDE brought the Dispose method for me. I am wondering what code i should write inside the Dispose method so that it will take care of my memory management or whatever stuff it should do.
public class ESNVerification : IDisposable
{
public bool Save(string a,string b)
{
//Save the data to table
return true;
}
public void Dispose()
{
throw new NotImplementedException();
// Really what i should do here ?
}
}
I recommend Three Simple Rules for how to implement IDisposable; simplified here:
Rule 1: Don't do it (unless you need to). There are only two situations when IDisposable does need to be implemented: The class owns an unmanaged resource or The class owns managed (IDisposable) resources.
Rule 2: For a class owning managed resources, implement IDisposable (but not a finalizer). This implementation of IDisposable should only call Dispose for each owned resource. The class should not have a finalizer.
Rule 3: For a class owning a single unmanaged resource, implement both IDisposable and a finalizer.
Unless you're working with some kind of un-managed resources that need to be cleaned up (which, in this extremely simple case, you're not) then you really have no reason to implement IDisposable.
The first line in the MSDN Description of the IDisposable Interface:
The primary use of this interface is
to release unmanaged resources
If you were using un-managed resources, then the Dispose method is where you would ensure that those resources are properly released (the Garbage Collector will take care of all of your managed resources for you).
Maybe you should take a look into these MSDN articles:
Implementing a Dispose Method
Implementing Finalize and Dispose to Clean Up Unmanaged Resources
The dispose method is there so that you can release any resources that you have allocated that is not simple objects (which are cleaned up by garbage collection). For example
streams
database connections and/or result sets
pointers to unmanaged objects
any object you have allocated inside your object that is also implementing IDisposable
should be cleaned up in your Dispose method (probably by calling Dispose on those objects).
If you do not have any such resources, you probably do not need to implement IDisposable. However, you might be implementing another interface which inherits from IDisposable, such as IEnumerator. In that case you can leave the Dispose method empty if you don't have any of the resources mentioned above.
Dispose is meant to release unmanaged resources (i.e. resources that are not automatically taken care of by the garbage collector after use). Common examples are database and file connections. Implementing a Dispose Method provides more explanation and a code example.
That page has an important warning that the Dispose method should be implemented in such a way that multiple calls do not throw an exception. That's because it's used by the garbage collector and it can call the dispose method multiple times.
In 99% of cases, Microsoft's framework is vastly overcomplicated, and the right approach is simple:
If your class has any fields of types that implement IDisposable, and nobody's going to expect to use those objects once you're done with them, you should implement IDisposable, and your disposal method should call Dispose on all such fields.
If your class has no such fields, but you think classes that derive from yours might, or if your class needs to implement an interface like IEnumerator(of T) which requires IDisposable, you should have an overridable Dispose method that does nothing.
The proper semantics for a "dispose" method is for an object to do whatever is necessary to clean up other objects before it is abandoned. If an object doesn't have to clean up other objects, Dispose should harmlessly do nothing. There is never any reason to throw NotImplementedException or NotSupportedException from Dispose.
The key point with implementing IDisposable isn't to clean up any particular kind of "resources", but rather to ensure that if an object changes other entities in the system in a way that needs to be cleaned up sometime, those entities will get cleaned up while the information and impetus necessary to do so still exists. Ideally, this cleanup should happen as soon as possible, but no sooner. If an object contains e.g. nothing but a bunch of arrays of strings, there will be no need for cleanup. A string does not require any cleanup; an array of objects not requiring cleanup does not require any cleanup, and an object holding nothing but other objects which don't require cleanup won't require cleanup either. On the other hand, an action like opening a TCP socket creates a need to ensure a certain cleanup action (closing the socket) will be performed. If an object opens a TCP socket and keeps information about it, the purpose of calling Dispose on that object wouldn't be to destroy the information about the socket (that will be taken care of by the garbage collector) but to ensure that the necessary "close" operation on the TCP stack gets performed.
There are two pretty decent articles on MSDN: Implementing a Dispose Method and Implementing Finalize and Dispose to Clean Up Unmanaged Resources. Having observed that the first one says:
There is no performance benefit in implementing the Dispose method on types that use only managed resources (such as arrays) because they are automatically reclaimed by the garbage collector.
I'd recommend you read both, decide whether you need to implement it, and use the code examples there as a start.
There are only two reasons to implement IDisposable1 and what you do inside the Dispose method depends on which one applies to your situation.
If your class creates object instances that implement IDisposable and cannot dispose of them in the method that creates them, that class will have member variables that reference those instances. That class should also implement IDisposable, and inside it you should call Dispose on each disposable member.
If you're using unmanaged resources directly, your class should implement IDisposable and in its Dispose method you should deal with them as needed. What exactly that means will vary a lot, since there's no standard interface available. This is very unusual - normally you use managed classes that handle those for you, like FileStream or SqlConnection. (In which case, see above.)
1 Unless you're deliberately using IDisposable in a non-standard way. (Though the merits of that are debatable.)
The IDisposable pattern should be used when your class use unmanaged system resources, as handles on files, pointers... To free the memory they use.
If your class doesn't use unmanaged resources, it probably don't need to use this pattern.
If you really needs IDisposable :
public void Dispose()
{
//Free here your resources
this.Dispose(true);
GC.SuppressFinalize(this);
}

Correct way of implementing Finalize and Dispose(When parent class implements IDisposable)

I was implementing Finalize and Dispose in my classes, I implemented IDisposable on my parent class and override the Dispose(bool) overload in my child classes. I was not sure
whether to use a duplicate isDisposed variable(as its already there in base class) or not?
Whether to implement a finalizer in child class too or not?
Both these things are done in example given here -
http://guides.brucejmack.biz/CodeRules/FxCop/Docs/Rules/Usage/DisposeMethodsShouldCallBaseClassDispose.html
Whereas example in this MSDN article doesn't have any of these two -
http://msdn.microsoft.com/en-us/library/b1yfkh5e.aspx
whereas this example in MSDN is not complete -
http://msdn.microsoft.com/en-us/library/ms182330.aspx
It's very rare for a finalizer to be useful. The documentation you link to isn't totally helpful - it offers the following rather circular advice:
Implement Finalize only on objects
that require finalization
That's an excellent example of begging the question, but it's not very helpful.
In practice, the vast majority of the time you don't want a finalizer. (One of the learning curves .NET developers have to go through is discovering that in most of the places they think they need a finalizer, they don't.) You've tagged this as (amongst other things) a WPF question, and I'd say it'd almost always be a mistake to put a finalizer on a UI object. (So even if you are in one of the unusual situations that turns out to require a finalizer, that work doesn't belong anywhere near code that concerns itself with WPF.)
For most of the scenarios in which finalizers seem like they might be useful, they turn out not to be, because by the time your finalizer runs, it's already too late for it to do anything useful.
For example it's usually a bad idea to try to do anything with any of the objects your object has a reference to, because by the time your finalizer runs, those objects may already have been finalized. (.NET makes no guarantees about the order in which finalizers run, so you simply have no way of knowing whether the objects you've got references to have been finalized.) It's bad idea to invoke a method on an object whose finalizer has already been run.
If you have some way of knowing that some object definitely hasn't been finalized, then it is safe to use it, but that's a pretty unusual situation to be in. (...unless the object in question has no finalizer, and makes use of no finalizable resources itself. But in that case, it's probably not an object you'd actually need to do anything to when your own object is going away.)
The main situation in which finalizers seem useful is interop: e.g., suppose you're using P/Invoke to call some unmanaged API, and that API returns you a handle. Perhaps there's some other API you need to call to close that handle. Since that's all unmanaged stuff, the .NET GC doesn't know what those handles are, and it's your job to make sure that they get cleaned up, at which point a finalizer is reasonable...except in practice, it's almost always best to use a SafeHandle for that scenario.
In practice, the only places I've found myself using finalizers have been a) experiments designed to investigate what the GC does, and b) diagnostic code designed to discover something about how particular objects are being used in a system. Neither kind of code should end up going into production.
So the answer to whether you need "to implement a finalizer in child class too or not" is: if you need to ask, then the answer is no.
As for whether to duplicate the flag...other answers are providing contradictory advice here. The main points are 1) you do need to call the base Dispose and 2) your Dispose needs to be idempotent. (I.e., it doesn't matter if it's called once, twice, 5 times, 100 times - it shouldn't complain if it's called more than once.) You're at liberty to implement that however you like - a boolean flag is one way, but I've often found that it's enough to set certain fields to null in my Dispose method, at which point that removes any need for a separate boolean flag - you can tell that Dispose was already called because you already set those fields to null.
A lot of the guidance out there on IDisposable is extremely unhelpful, because it addresses the situation where you need a finalizer, but that's actually a very unusual case. It means that lots of people write a IDisposable implementations that are far more complex than necessary. In practice, most classes call into the category Stephen Cleary calls "level 1" in the article that jpierson linked to. And for these, you don't need all the GC.KeepAlive, GC.SuppressFinalize, and Dispose(bool) stuff that clutters up most of the examples. Life's actually much simpler most of the time, as Cleary's advice for these "level 1" types shows.
Duplicate is needed
If you don't have any clean-up in child class simply call base.Dispose() and if there are some class level clean-up, do it after a call to base.Dispose(). You need to separate state of these two classes so there should be a IsDisposed boolean for each class. This way you can add clean-up code whenever you need.
When you determine a class as IDisposable, you simply tell GC I'm taking care of it's clean-up procedure and you should SuppressFinilize on this class so GC would remove it from it's queue. Unless you call GC.SupressFinalize(this) nothing happens special to an IDisposable class. So if you implement it as I mentioned there's no need for a Finilizer since you just told GC not to finalize it.
The correct way to implement IDisposable depends on whether you have any unmanaged resources owned by your class. The exact way to implement IDisposable is still something not all developers agree on and some like Stephen Cleary have strong opinions on the disposable paradigm in general.
see: Implementing Finalize and Dispose to Clean Up Unmanaged Resources
The documentation for IDisposable interface also explains this breifly and this article points out some of the same things but also on MSDN.
As far as whether a duplicate boolean field "isDisposed" is required in the base class. It appears that this is mainly just a useful convention that can be used when a subclass itself may add additional unmanaged resources that need to be disposed of. Since Dispose is declared virtual, calling Dispose on a subclass instance always causes that class's Dispose method to be called first which in turn calls base.Dispose as it's last step giving a chance to clear up each level in the inheritance hierarchy. So I would probably summarize this as, if your subclass has additional unmanaged resources above what is owned by the base then you will probably be best to have your own boolean isDisposed field to track it's disposal in a transactional nature inside it's Dispose method but as Ian mentions in his answer, there are other ways to represent an already-disposed state.
1) No need to duplicate
2) Implementing a finalizer will help to dispose items that are not explicitly disposed. But is not guaranteed. It is a good practice to do.
Only implement a finalizer if an object holds information about stuff needing cleanup, and this information is in some form other than Object references to other objects needing cleanup (e.g. a file handle stored as an Int32). If a class implements a finalizer, it should not hold strong Object references to any other objects which are not required for cleanup. If it would hold other references, the portion responsible for cleanup should be split off into its own object with a finalizer, and the main object should hold a reference to that. The main object should then not have a finalizer.
Derived classes should only have finalizers if the purpose of the base class was to support one. If the purpose of a class doesn't center around a finalizer, there's not much point allowing a derived class to add one, since derived classes almost certainly shouldn't (even if they need to add unmanaged resources, they should put the resources in their own class and just hold a reference to it).

Can I a implement DisposeBase abstract class?

Is there a catch or hidden problem in using a DisposableBase base class instead of recoding the Dispose pattern on every class?
Why aren't everyone using such a relevant class?
Edits:
I naturally only meant classes that implement IDisposable
I know it uses up the option for inheritance, but I'm willing to pay the price (at least when I can and it doesn't hurt me otherwise).
When I can seal the class, I do - but I have some cases where I want the base of an inheritance hierarchy to be Disposable.
You don't need to implement Dispose() on every class - just those with something that needs deterministic cleanup. Re a Disposable base-class, I'm not entirely sure it provides a whole lot - IDisposable isn't a complex interface. The main time it might be useful is if you are handling unmanaged resources and want a finalizer, but even then it isn't much code.
Personally, I wouldn't bother with such a base class. In particular, inheritance (in a single-inheritance world) gets restrictive very quickly. But more to the point, overriding a method isn't much different to simply providing a public Dispose() method.
Again: you only need a finalizer etc if you are handling unmanaged objects.
If I had a lot of these (unmanaged resouces), I might see whether I could get PostSharp to do the work for me. I don't know if one already exists, but it might be possible to create an aspect that handles (in particular) the finalizer etc. Who knows...
Well, it uses up your one option for inheritance to describe a single aspect of your class - that's not ideal, IMO. It would be interesting to try to do something with composition, where you have a reference to a DisposableHelper and the implementation of IDisposable just calls helper.Dispose, which has the rest of the boilerplate logic in - and can call back to your code via a callback delegate. Hmm. Subclasses could subscribe to a protected Disposing event to register "I need to do something"... it might be worth looking at some time.
Personally I don't find myself implementing IDisposable often enough to make it an issue - and when I do, I typically seal my classes anyway, so half of the stuff in the pattern becomes a non-issue.
As Marc Gravell said, you only need a finalizer if you are handling unmanaged objects. Introducing an unnecessary finalizer in a base class is a bad idea, as per the reasons in section 1.1.4 of the Dispose, Finalization, and Resource Management guidelines:
There is a real cost associated with
instances with finalizers, both from a
performance and code complexity
standpoint. ... Finalization increases the cost and duration of
your object’s lifetime as each
finalizable object must be placed on a
special finalizer registration queue
when allocated, essentially creating
an extra pointer-sized field to refer
to your object. Moreover, objects in
this queue get walked during GC,
processed, and eventually promoted to
yet another queue that the GC uses to
execute finalizers. Increasing the
number of finalizable objects directly
correlates to more objects being
promoted to higher generations, and an
increased amount of time spent by the
GC walking queues, moving pointers
around, and executing finalizers.
Also, by keeping your object’s state
around longer, you tend to use memory
for a longer period of time, which
leads to an increase in working set.
If you use SafeHandle (and related classes), it's unlikely that any classes that derive from DisposableBase would ever need to be finalized.

Categories

Resources