Should I Treat Entity Framework as an Unmanaged Resource? - c#

I am working with a class that uses a reference to EF in its constructor.
I have implemented IDisposable, but I'm not sure if I need a destructor because I'm not certain I can classify EF as an unmanaged resource.
If EF is a managed resource, then I don't need a destructor, so I think this is a proper example:
public ExampleClass : IDisposable
{
public ExampleClass(string connectionStringName, ILogger log)
{
//...
Db = new Entities(connectionStringName);
}
private bool _isDisposed;
public void Dispose()
{
if (_isDisposed) return;
Db.Dispose();
_isDisposed= true;
}
}
If EF is unmanaged, then I'll go with this:
public ExampleClass : IDisposable
{
public ExampleClass(string connectionStringName, ILogger log)
{
//...
Db = new Entities(connectionStringName);
}
public void Dispose()
{
Dispose(true);
}
~ExampleClass()
{
Dispose(false);
}
private bool _isDisposed;
protected virtual void Dispose(bool disposing)
{
if (_isDisposed) return;
// Dispose of managed resources
if (disposing)
{
// Dispose of managed resources; assumption here that EF is unmanaged.
}
// Dispose of unmanaged resources
Db.Dispose();
_isDisposed = true;
//freed, so no destructor necessary.
GC.SuppressFinalize(this);
}
}
Which one is it?

You would never want to use a finalizer (destructor) in this case.
Whether DbContext contains unmanaged resources or not, and even whether it responsibly frees those unmanaged resources or not, is not relevant to whether you can try to invoke DbContext.Dispose() from a finalizer.
The fact is that, any time you have a managed object (which an instance of DbContext is), it is never safe to attempt to invoke any method on that instance. The reason is that, by the time the finalizer is invoked, the DbContext object may have already been GC-collected and no longer exist. If that were to happen, you would get a NullReferenceException when attempting to call Db.Dispose(). Or, if you're lucky, and Db is still "alive", the exception can also be thrown from within the DbContext.Dispose() method if it has dependencies on other objects that have since been finalized and collected.
As this "Dispose Pattern" MSDN article says:
X DO NOT access any finalizable objects in the finalizer code path, because there is significant risk that they will have already been finalized.
For example, a finalizable object A that has a reference to another finalizable object B cannot reliably use B in A’s finalizer, or vice versa. Finalizers are called in a random order (short of a weak ordering guarantee for critical finalization).
Also, note the following from Eric Lippert's When everything you know is wrong, part two:
Myth: Finalizers run in a predictable order
Suppose we have a tree of objects, all finalizable, and all on the finalizer queue. There is no requirement whatsoever that the tree be finalized from the root to the leaves, from the leaves to the root, or any other order.
Myth: An object being finalized can safely access another object.
This myth follows directly from the previous. If you have a tree of objects and you are finalizing the root, then the children are still alive — because the root is alive, because it is on the finalization queue, and so the children have a living reference — but the children may have already been finalized, and are in no particularly good state to have their methods or data accessed.
Something else to consider: what are you trying to dispose? Is your concern making sure that database connections are closed in a timely fashion? If so, then you'll be interested in what the EF documentation has to say about this:
By default, the context manages connections to the database. The context opens and closes connections as needed. For example, the context opens a connection to execute a query, and then closes the connection when all the result sets have been processed.
What this means is that, by default, connections don't need DbContext.Dispose() to be called to be closed in a timely fashion. They are opened and closed (from a connection pool) as queries are executed. So, though it's still a very good idea to make sure you always call DbContext.Dispose() explicitly, it's useful to know that, if you don't do it or forget for some reason, by default, this is not causing some kind of connection leak.
And finally, one last thing you may want to keep in mind, is that with the code you posted that doesn't have the finalizer, because you instantiate the DbContext inside the constructor of another class, it is actually possible that the DbContext.Dispose() method won't always get called. It's good to be aware of this special case so you are not caught with your pants down.
For instance, suppose I adjust your code ever so slightly to allow for an exception to be thrown after the line in the constructor that instantiates the DbContext:
public ExampleClass : IDisposable
{
public ExampleClass(string connectionStringName, ILogger log)
{
//...
Db = new Entities(connectionStringName);
// let's pretend I have some code that can throw an exception here.
throw new Exception("something went wrong AFTER constructing Db");
}
private bool _isDisposed;
public void Dispose()
{
if (_isDisposed) return;
Db.Dispose();
_isDisposed= true;
}
}
And let's say your class is used like this:
using (var example = new ExampleClass("connString", log))
{
// ...
}
Even though this appears to be a perfectly safe and clean design, because an exception is thrown inside the constructor of ExampleClass after a new instance of DbContext has already been created, ExampleClass.Dispose() is never invoked, and by extension, DbContext.Dispose() is never invoked either on the newly created instance.
You can read more about this unfortunate situation here.
To ensure that the DbContext's Dispose() method is always invoked, no matter what happens inside the ExampleClass constructor, you would have to modify the ExampleClass class to something like this:
public ExampleClass : IDisposable
{
public ExampleClass(string connectionStringName, ILogger log)
{
bool ok = false;
try
{
//...
Db = new Entities(connectionStringName);
// let's pretend I have some code that can throw an exception here.
throw new Exception("something went wrong AFTER constructing Db");
ok = true;
}
finally
{
if (!ok)
{
if (Db != null)
{
Db.Dispose();
}
}
}
}
private bool _isDisposed;
public void Dispose()
{
if (_isDisposed) return;
Db.Dispose();
_isDisposed= true;
}
}
But the above is really only a concern if the constructor is doing more than just creating an instance of a DbContext.

C# provides garbage collection and thus does not need an explicit destructor. If you do control an unmanaged resource, however, you will need to explicitly free that resource when you are done with it. Implicit control over this resource is provided with a Finalize( ) method (called a finalizer), which will be called by the garbage collector when your object is destroyed.
https://www.oreilly.com/library/view/programming-c/0596001177/ch04s04.html

Related

Do I need to use IDisposable?

Trying to see if I'm understanding this correctly. Regretfully, I have not had much experience with IDisposable. We're taking a static class and making it non-static, and it's a class that is responsible for making web service calls in our Xamarin app. This is an object that I would definitely not want lying around. So I thought of having this class inherit the IDisposable interface, and I implemented the methods as shown:
private bool disposedValue = false; // To detect redundant calls
protected virtual void Dispose(bool disposing)
{
if (!disposedValue)
{
if (disposing)
{
// TODO: dispose managed state (managed objects).
}
// TODO: free unmanaged resources (unmanaged objects) and override a finalizer below.
// TODO: set large fields to null.
disposedValue = true;
}
}
// TODO: override a finalizer only if Dispose(bool disposing) above has code to free unmanaged resources.
//~WebServiceCallThread()
//{
// // Do not change this code. Put cleanup code in Dispose(bool disposing) above.
// Dispose(false);
//}
// This code added to correctly implement the disposable pattern.
public void Dispose()
{
// Do not change this code. Put cleanup code in Dispose(bool disposing) above.
Dispose(true);
// TODO: uncomment the following line if the finalizer is overridden above.
//GC.SuppressFinalize(this);
}
So all this appears to be doing, is when the Dispose method is called, it then calls a virtual method declared above Dispose(bool disposing). With the current code, this does nothing (or so it seems). It just calls the Dispose method, in which that calls an overloaded Dispose method. But nothing is actually "disposing" here.
Upon some research, it SEEMS (which is why I'm asking, I think I am understanding correctly). It says to only call a finalizer and the GC.SuppressFinalize method IF you dispose managed state. I'm not sure what dispose manage state means, but there's really nothing in my class that needs to get disposed. I just have some global int variables but that's it. The meat of this class is a HttpCall, which is already disposing of the objects automatically, as such:
public async void CallUrlHttpClient(string URL, System.Action<string> Handler, System.Action<XamarinMobile.Classes.ErrorInfo> ErrorHandler, int Tries)
{
var result = string.Empty;
didMakeSuccessfulHttpRequest = false;
using (var client = new HttpClient())
{
... Code
}
...
So here's my question: Do I need to make this class inherit IDisposable? I'm wondering if anything under the hood is being done, but it seems as if my application can just utilize the GC to get rid of my object that uses this class. This is how I call it, via a static method in a class:
public static void WebServiceCall(string URL, System.Action<string> Callback)
{
//using (XamarinMobile.Classes.WebServiceCallThread thread = new Classes.WebServiceCallThread())
//{
XamarinMobile.Classes.WebServiceCallThread thread = new Classes.WebServiceCallThread();
thread.Call(URL, Callback);
//}
}
If I understand GC correctly, once this method is done, the GC will automatically get rid of the object. So do I really need to make the WebServiceCallThread inherit from IDisposable? Again I'm asking if I'm understanding all of this correctly. If not, please correct me and let me know where I'm confused.
Thanks.
If your class created the HttpClient and maintained that instance throughout its own lifetime (like an instance variable) then you would implement IDisposable, and your Dispose() method would dispose the HttpClient.
That would be this part:
if (disposing)
{
// TODO: dispose managed state (managed objects).
_httpClient.Dispose();
}
In this case you don't need to implement IDisposable because you're already disposing your HttpClient.
using (var client = new HttpClient())
{
... Code
}
However, it's actually recommended to create one instance of HttpClient per endpoint and reuse it instead of creating and disposing over and over. (If your load isn't that heavy then you likely won't see any bad effect from creating and disposing HttpClient.)
If you wanted to avoid creating and disposing HttpClient then you could implement IDisposable. Your class either creates or receives (in its constructor) an instance of HttpClient. Now that your class "owns" that instance which needs to be disposed when your class is disposed, you make your class IDisposable. That way others know that your class needs to be disposed. They might not know why, but that's not important. By making it IDisposable you're letting them know that they need to dispose it. And then when your class is disposed it cleans up its HttpClient.
Just to provide a little extra clarification:
Many people, if asked "What is IDisposable for?" will answer that it causes garbage collection. It actually has nothing to do with garbage collection. Objects always get garbage collected when they're out of scope (there are no more references to them) whether or not they implement IDisposable.
The confusion comes up because the recommended pattern for IDisposable (which you referenced) includes a finalizer (~WebServiceCallThread()) which is called when the object is garbage collected.
When the garbage collector removes an item it calls the finalizer if there is one. This pattern causes the finalizer to call Dispose() if it hasn't been called already, just as a backup in case someone didn't call Dispose() like they were supposed to. But it's no substitute for calling Dispose() because there's no telling when the garbage collector will collect the item. There's a chance the finalizer will never get called. It's just a backup to try to mitigate what happens if consumers don't call Dispose().

Does C# automatically "dispose" IDisposable objects when they fall out of scope?

I am writing an ASP.NET Web API. As part of this, I have a class that I found out, during testing, needs to implement IDisposable to ensure managed resources are freed.
So, I implemented IDisposable in my class, and put the code necessary to free the resources in the Dispose() method.
There are many places in my code (hundreds) where I instantiate this object, and in the same line call a method on the new instance. I only need the instance to call the single method.
Here's an example:
// MyObject, a class that needs to be disposed of.
public class MyObject : IDisposable
{
private AnObjectThatMustBeDisposed localObject;
public MyObject()
{
localObject = SomeLibrary.SomeProject.AnObjectThatMustBeDisposed.Create();
}
public void doOperationOne()
{
localObject.DoSomething(1);
}
public string getOperationTwo()
{
return localObject.DoSomething(2);
}
public string getOperationThree()
{
return localObject.DoSomething(3);
}
public bool getOperationFour(string input)
{
return localObject.DoSomethingSpecial(4,input.ToLower());
}
...
public void getOperationOneHundred(DateTime input)
{
localObject.DoSomethingElse(100,input);
}
public void Dispose()
{
localObject.CloseResources();
localObject.FreeUpMemory();
localObject.Close();
localObject.Dispose();
}
}
// A class that makes use of MyObject
public class MyLibraryThatUsesMyObject
{
public void Method1()
{
new MyObject().doOperationOne();
}
public string Method2()
{
return new MyObject().getOperationTwo();
}
public int Method3()
{
return new MyObject().getOperationThree();
}
public bool Method4(string testString)
{
if (testString.Length > 6)
{
if (new MyObject().getOperationFour(testString)) return true;
else return false;
}
else return false;
}
...
public void Method100()
{
new MyObject().doOperationOneHundred(DateTime.Now);
}
}
My question is: Does .NET automatically Dispose() objects when they fall out of scope? Or, do I actually have to do this...
public void Method1()
{
using (MyObject o = new MyObject())
{
o.DoOperationOne();
}
}
...to each method? It wouldn't be hard if I had two or three methods, but if I have tons of methods, this refactoring could take quite a while.
I am not sure how ASP.NET handles requests as they complete - i.e. does the framework give code time to Dispose() things, or does it "cut off" execution as soon as the return is called, not letting things dispose?
The fact that, without implementing IDisposable myself, things inside the MyObject class are failing due to unreleased resources causing leaks, it feels like .NET does not automatically Dispose things. So, if that's the case, can I do something so I don't have to refactor hundreds of methods?
EDIT: I tried simply implementing IDisposable, but my unit test was still able to produce a resource leak. So it would appear that my suspicion that .NET is not automatically disposing is correct. So now my question becomes - how can I force disposing without having to refactor hundreds of methods?
Dispose is not automatically called. If you don't call .Dispose() (either explicitly or via a using statement) the method will never be called.
The only caveat is methods that are implemented with the pattern
public void Dispose()
{
GC.SuppressFinalize(this);
Dispose(true);
}
~MyClass()
{
Dispose(false);
}
bool _isDisposed = false;
protected virtual void Dispose(bool disposeing)
{
if(_isDisposed)
return;
_isDisposed = true;
if(disposing)
{
//Disposed managed code here
}
//Dispose unmanaged code only here.
}
Will have Dispose(false) called on it when the object is finalized, but you are not allowed to dispose (or even access) managed objects (i.e: other stuff that implements .Dispose()) when disposing is false.
You will need to refactor your code if you want your resources disposed correctly.
There is a really, really, good article written by Stephen Cleary "IDisposable: What Your Mother Never Told You About Resource Deallocation" that does a very good job explaining how Dispose works and how to correctly write your own disposeable objects (for example, that caveat pattern I mentioned above is recommended by Microsoft but is actually a very bad pattern to do. Classes should only either hold only managed resources or derive from SafeHandle and only hold a unmanaged resource and possibly other SafeHandles. You should never have a class that holds both managed and unmanaged resources nor a single class that holds multiple unmanaged resources)
I am not sure how ASP.NET handles requests as they complete - i.e. does the framework give code time to Dispose() things, or does it "cut off" execution as soon as the return is called, not letting things dispose?
My answer will try to answer the specific question above since you have gotten answers to your other question.
In the MVC framework there is a class that creates controllers. It is named DefaultControllerFactory and it has this method:
public virtual void ReleaseController(IController controller)
{
IDisposable disposable = controller as IDisposable;
if (disposable != null)
{
disposable.Dispose();
}
}
Please note the parameter the above method takes: IController. So it passes your controller class (since all controllers implement that interface by deriving the BaseController) and if your controller implements IDisposable, it will cast it and then call Dispose() on it. So it gives you a chance to do whatever cleanup you need to do.
If you have rolled your own factory and you are not using the DefaultControllerFactory then make sure you are doing the same thing and calling the Dispose on the controllers.
I got the above source code from MVC's Source Code.
No, the CLR will never call Dispose on your object when it goes out of scope. It will, however, call the class destructor (which must be explicitly overridden).
Note: The example that follows is not intended to demonstrate what should be done in production code. The practice recommended by Microsoft can be found here.
Consider the following class:
public class MyDisposable : IDisposable
{
public void Dispose()
{
Console.WriteLine("Disposing this disposable instance.");
}
// Note: This is not the right way to implement the dispose pattern. See
// the MSDN docs on the recommended pattern.
~MyDisposable()
{
Dispose();
}
}
Now, write this method that uses it, and set a break point on the MyDispoable.Dispose method.
private static void UseDisposable()
{
Console.WriteLine("Enter");
var disposable = new MyDisposable();
Console.WriteLine("Exit");
}
What you will see is that, when leaving this scope, not only is the Dispose on MyDisposable not called, but neither is the finalizer. This means that the locals you created in your method are not cleaned up at the end of the method, even though they are local to the method (not returned to the caller, or assigned to any other object).
The way that I try to manage disposable resources in my own code is by not using them as class members, and put all of them inside of using blocks. Keep in mind that a using is only syntax sugar for try {...} finally {...}, though.
Now, I think your question is more about ASP.NET than the CLR. I am not sure of what ASP.NET does to handle disposable resources. If you are using the resource inside of a Controller, then have a go at implementing IDisposable, and using a break point to see if it gets called after your method completes. That's a scenario that I'm not as sure about, but it seems reasonable that it may work that way.
Edit: See the answer from #CodingYoshi regarding ASP.NET controller disposal. It turns out that MVC will deterministically call dispose, so some of your refactoring may be slightly more simple.

Why disposed object doesn't throw exception on using it after disposing?

Is it legal to call a method on disposed object? If yes, why?
In the following demo program, I've a disposable class A (which implements IDisposable interface).As far as I know, if I pass disposable object to using() construct, then Dispose() method gets called automatically at the closing bracket:
A a = new A();
using (a)
{
//...
}//<--------- a.Dispose() gets called here!
//here the object is supposed to be disposed,
//and shouldn't be used, as far as I understand.
If that is correct, then please explain the output of this program:
public class A : IDisposable
{
int i = 100;
public void Dispose()
{
Console.WriteLine("Dispose() called");
}
public void f()
{
Console.WriteLine("{0}", i); i *= 2;
}
}
public class Test
{
public static void Main()
{
A a = new A();
Console.WriteLine("Before using()");
a.f();
using ( a)
{
Console.WriteLine("Inside using()");
a.f();
}
Console.WriteLine("After using()");
a.f();
}
}
Output (ideone):
Before using()
100
Inside using()
200
Dispose() called
After using()
400
How can I call f() on the disposed object a? Is this allowed? If yes, then why? If no, then why the above program doesn't give exception at runtime?
I know that the popular construct of using using is this:
using (A a = new A())
{
//working with a
}
But I'm just experimenting, that is why I wrote it differently.
Disposed doesn't mean gone. Disposed only means that any unmanaged resource (like a file, connection of any kind, ...) has been released. While this usually means that the object doesn't provide any useful functionality, there might still be methods that don't depend on that unmanaged resource and still work as usual.
The Disposing mechanism exist as .net (and inheritly, C#.net) is a garbage-collected environment, meaning you aren't responsable for memory management. However, the garbage collector can't decide if an unmanaged resource has been finished using, thus you need to do this yourself.
If you want methods to throw an exception after the object has been diposed, you'll need a boolean to capture the dispose status, and once the object is disposed, you throw the exception:
public class A : IDisposable
{
int i = 100;
bool disposed = false;
public void Dispose()
{
disposed = true;
Console.WriteLine("Dispose() called");
}
public void f()
{
if(disposed)
throw new ObjectDisposedException();
Console.WriteLine("{0}", i); i *= 2;
}
}
The exception is not thrown because you have not designed the methods to throw ObjectDisposedException after Dispose has been called.
The clr does not automagically know that it should throw ObjectDisposedException once Dispose is called. It's your responsibility to throw an exception if Dispose has released any resources needed for successful execution of your methods.
A typical Dispose() implementation only calls Dispose() on any objects that it stores in its fields that are disposable. Which in turn release unmanaged resources. If you implement IDisposable and not actually do anything, like you did in your snippet, then the object state doesn't change at all. Nothing can go wrong. Don't mix up disposal with finalization.
The purpose of IDisposable is to allow an object to fix the state of any outside entities which have, for its benefit, been put into a state that is less than ideal for other purposes. For example, an Io.Ports.SerialPort object might have changed the state of a serial port from "available for any application that wants it" to "only usable by one particular Io.Ports.SerialPort object"; the primary purpose of SerialPort.Dispose is to restore the state of the serial port to "available for any application".
Of course, once an object that implements IDisposable has reset entities that had been maintaining a certain state for its benefit, it will no longer have the benefit of those entities' maintained state. For example, once the state of the serial port has been set to "available for any application", the data streams with which it had been associated can no longer be used to send and receive data. If an object could function normally without outside entities being put into a special state for its benefit, there would be no reason to leave outside entities in a special state in the first place.
Generally, after IDisposable.Dispose has been called on an object, the object should not be expected to be capable of doing much. Attempting to use most methods on such an object would indicate a bug; if a method can't reasonably be expected to work, the proper way to indicate that is via ObjectDisposedException.
Microsoft suggests that nearly all methods on an object which implements IDisposable should throw ObjectDisposedException if they are used on an object which has been disposed. I would suggest that such advice is overbroad. It is often very useful for devices to expose methods or properties to find out what happened while the object was alive. Although one could give a communications class a Close method as well as a Dispose method, and only allow one to query things like NumberOfPacketsExchanged after a close but not after a Dispose, but that seems excessively complicated. Reading properties related to things that happened before an object was Disposed seems a perfectly reasonable pattern.
Calling Dispose() doesn't set the object reference to null, and your custom disposable class doesn't contain any logic to throw an exception if its functions are accessed after Dispose() has been called so it is of course legal.
In the real world, Dispose() releases unmanaged resources and those resources will be unavailable thereafter, and/or the class author has it throw ObjectDisposedException if you try to use the object after calling Dispose(). Typically a class-level boolean would be set to true within the body of Dispose() and that value checked in the other members of the class before they do any work, with the exception being thrown if the bool is true.
A disposer in C# is not the same as a destructor in C++. A disposer is used to release managed (or unmanaged) resources while the object remains valid.
Exceptions are thrown depending on the implementation of the class. If f() does not require the use of your already disposed objects, then it doesn't necessarily need to throw an exception.

C# disposable objects

Are there some advices about how I should deal with the IDisposable object sequences?
For example, I have a method that builds a IEnumerable<System.Drawing.Image> sequence and
at some point I would need to dispose that objects manually, because otherwise this might lead to some leaks.
Now, is there a way to bind the Dispose() call to garbage collector actions, because I want these objects disposed right in the moment they are no longer accessible from other code parts?
**Or maybe you could advice me some other approach? **
Generally, this seems to be the same problem as it comes, for example, in unmanaged C++ without shared pointers, where you can have a method:
SomeObject* AllocateAndConstruct();
and then you can't be sure when to dispose it, if you don't use code contracts or don't state something in the comments.
I guess the situation with disposable objects is pretty the same, but I hope there is an appropriate solution for this.
(from the question)
Now, is there a way to bind the
Dispose() call to garbage collector
actions, because I want these objects
disposed right in the moment they are
no longer accessible from other code
parts?
GC doesn't happen immediately when your object goes out of scope / reach; it is non-deterministic. By the time GC sees it, there is no point doing anything else (that isn't already handled by the finalizer), as it is too late.
The trick, then, is to know when you are done with it, and call Dispose() yourself. In many cases using achieves this. For example you could write a class that implements IDisposable and encapsulates a set of Images - and wrap your use of that encapsulating object with using. The Dispose() on the wrapper could Dispose() all the images held.
i.e.
using(var imageWrapper = GetImages()) {
foreach(var image in imageWrapper) {
...
}
// etc
} // assume imageWrapper is something you write, which disposes the child items
however, this is a bit trickier if you are displaying the data on the UI. There is no shortcut there; you will have to track when you are done with each image, or accept non-deterministic finalization.
If you want to determiniscally dispose of the objects in the collection, you should call Dispose on each:
myImages.ToList().ForEach(image => image.Dispose());
If you don't do this, and if your objects become unreachable, the GC will eventually run and release them.
Now, if you don't want to manually code the Dispose calls, you can create a wrapper class that implements IDisposable and use it through a using statement:
using (myImages.AsDisposable()) {
// ... process the images
}
This is the needed "infrastructure":
public class DisposableCollectionWrapper<D> : IDisposable
where D : IDisposable {
private readonly IEnumerable<D> _disposables;
public DisposableCollectionWrapper(IEnumerable<D> disposables) {
_disposables = disposables;
}
public void Dispose() {
if (_disposables == null) return;
foreach (var disposable in _disposables) {
disposable.Dispose();
}
}
}
public static class CollectionExtensions {
public static IDisposable AsDisposable<D>(this IEnumerable<D> self)
where D : IDisposable {
return new DisposableCollectionWrapper<D>(self);
}
}
Also notice that this is not the same as the situation you described with C++. In C++, if you don't delete your object, you have a genuine memory leak. In C#, if you don't dispose of your object, the garbage collector will eventually run and clean it up.
You should design your system in a way that you know when the resources are no longer needed. In the worst case, they'll be eventually disposed when the garbage collector gets to it, but the point of IDisposable is that you can release important resources earlier.
This "earlier" is up to you to define, for example, you can release them when the window that's using them closes, or when your unit of work finishes doing whatever operations on them. But at some point, some object should "own" these resources, and therefore should know when they're no longer needed.
You can use the 'using' block, to make sure, the IDisposable is disposed as soon the block is left. The compiler does encapsulate such blocks into try - finally statements in order to make sure, Dispose is called in any case when leaving the block.
By using a finalizer, one can make the GC call the Dispose method for those objects which where "missed" somehow. However, implementing a finalizer is more expensive and decreases the garbage collection efficiency - and possibly the overall performance of your application. So if any possible, you should try to make sure to dispose your IDisposables on your own; deterministically:
public class Context : IDisposable {
List<IDisposable> m_disposable = new List<IDisposable>();
public void AddDisposable(IDisposable disposable) {
m_disposable.Add(disposable);
}
public void Dispose() {
foreach (IDisposable disp in m_disposable)
disp.Dispose();
}
// the Context class is used that way:
static void Main(string[] args) {
using (Context context = new Context()) {
// create your images here, add each to the context
context.AddDisposable(image);
// add more objects here
} // <- leaving the scope will dispose the context
}
}
By using some clever design, the process of adding objects to the context may can get even more easier. One might give the context to the creation method or publish it via a static singleton. That way, it would be available for child method scopes as well - without having to pass a reference to the contex around. By utilizing that scheme it is even possible, to simulate an artificial destructor functionality like f.e. known from C++.
The neat method would be to create your own generic collection class that implements IDisposable. When this collection class is Disposed() ask for each element if it implements IDisposed, and if so Dispose it.
Example (look elsewhere if you don't know about the IDisposable pattern)
public class MyDisposableList<T> : List<T> : IDisposable
{
private bool disposed = false;
~MyDisposableList()
{
Dispose(false);
}
public void Dispose()
{
Dispose(true);
}
protected void Dispose(bool disposing)
{
if (!disposed)
{
foreach (T myT in this)
{
IDisposable myDisposableT = myT as IDisposable;
if (myDisposableT != null)
{
myDisposableT.Dispose();
}
myT = null;
}
this.Clear();
this.TrimExcess();
disposed = true;
}
}
...
}
usage:
using (MyDisposableList<System.Drawing.Bitmap> myList = new ...)
{
// add bitmaps to the list (bitmaps are IDisposable)
// use the elements in the list
}
The end of the using statement automatically Disposes myList, and thus all bitMaps in myList
By the way: if you loaded the bitmap from a file and you forgot to Dispose() the bitmap you don't know when you can delete that file.
You can call GC.Collect() if you really was to dispose those objects right away but to my understanding it is up to the GC to decide whether to collect the memory.
This in turn will call the Finalize() method for each object that should be released.
Note that if the collection goes out of scope the GC will eventually collect the memory used by the images.
You can also use the using construct if you use a collection that implements IDisposeable. That will guarantee that the objects will be disposed exactly when the collection goes out of scope (or nearly after the end of the scope).

Why is calling Dispose() in a finalizer causing an ObjectDisposedException?

I have a NHibernate repository that looks like this:
public class NHibRepository : IDisposable
{
public ISession Session { get; set; }
public ITransaction Transaction { get; set; }
// constructor
public NHibRepository()
{
Session = Database.OpenSession();
}
public IQueryable<T> GetAll<T>()
{
Transaction = Session.BeginTransaction();
return Session.Query<T>();
}
public void Dispose()
{
if (Transaction != null && Transaction.IsActive)
{
Transaction.Rollback(); // ObjectDisposedException on this line
}
Session.Close();
Session.Dispose();
}
~NHibRepository()
{
Dispose();
}
}
When I use the repository like this, it runs fine:
using (var repo = new NHibRepository())
{
Console.WriteLine(repo.GetAll<Product>().Count());
}
But when I use it like this, it will throw an ObjectDisposedException:
var repo = new NHibRepository();
Console.WriteLine(repo.GetAll<Product>().Count());
The easy solution would be to always dipose of the repository explicitly, but unfortunately I don't control the life cycle of some of the classes that use the repository.
My question is, why is the Transaction disposed of already even though I did not explicitly call Dispose()? I'd like to have the repository automatically clean itself up if it was not disposed explicitly.
My question is, why is the Transaction disposed of already even though I did not explicitly call Dispose()?
Perhaps the transaction's finalizer ran first. Remember, finalizers of dead objects can run in any order and on any thread, and need not have been correctly initialized before they are finalized. If you do not understand all the rules of finalizers then learn them all before you attempt to write any more code that uses finalizers. This is one of the hardest things to get right.
It also looks as though you have implemented the disposable pattern incorrectly, and that is going to cause you a world of grief. Read up on the pattern and do it correctly; the finalizer should not be disposing stuff that has already been disposed:
http://msdn.microsoft.com/en-us/magazine/cc163392.aspx
Lose the finalizer. Very few classes in .net need a finalizer; generally, the only .net 2.0-or-later classes which should have finalizers are those whose sole reason for existence revolves around it.
If a class with a finalizer holds an access to some other object, one of three conditions will apply when the finalizer is run:
The other object has already had its finalizer (if any) run; there's no need to dispose it, since it's already been taken care of.
The other object has a finalizer scheduled to run; there's no need to dispose it, since it will be taken care of automatically.
A reference to the other object exists outside of the object whose finalizer is running; this usually means one shouldn't dispose it.
The only time a finalizer should ever take any action to dispose of a manage object is when an outside reference is likely to exist, and the collection of the object being finalized will imply that the other object should be disposed despite the existence of that reference. That's a very rare situation.
You should put GC.SuppressFinalize(this); in your Dispose method otherwise the finalizer will dispose an already disposed object. Also, finalizers are in almost all cases only needed for unmanaged resources
The CLR makes no guarantees with respect to the order that finalizers will be called. It sees a group of unrooted objects (e.g. not reachable from any GC root) and starts calling finalizers. It doesn't matter that you have connections within your object graph. The finalizers can be called in any order. Finalizers are intended to clean up unmanaged resources, not child objects. You need to re-think your API.
You should have a member variable isDisposed that would be set to true by the Dispose() method. Then at the beginning of the Dispose() command just return if it is already set to true. According to .NET whitepapers, Dispose() could be executed multiple times without side effects and this is the way to do it.
Secondly, make the class sealed (the class is not implementing the Dispose pattern properly for inheritance) and put GC.SuppressFinalize(this); right before the isDisposed variable assignment in the Dispose() method.
public void Dispose()
{
if (isDisposed) { return; }
....
GC.SuppressFinalize(this);
isDisposed = true;
}

Categories