Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Does disposing an object dispose of all IDisposable properties?

This is a general question, though I do have a specific instance that I'm looking at, so I'm trying to keep the title and tags as generic as possible.

I'm doing an MVC project with IOC. My concrete Repositories implement IDisposable to dispose of the Ef Context.

Obviously, MVC handles disposing of the Controller object that gets called. However, do I need to override Dispose() in the controller? Here's my code as of right now:

private IUserRepository repository;

public UserController(IUserRepository repository) {
    this.repository = repository;
}

protected override void Dispose(bool disposing) {
    if (repository is IDisposable && repository != null) {
        (repository as IDisposable).Dispose();
        repository = null;
    }

    base.Dispose(disposing);
}

I check to make sure that it implements IDisposable as the Mocks from my Unit Tests I don't think implement it.

Now, my question is is that override of the Dispose() redundant? When the Controller (or any other object) gets disposed, does the Garbage Collector look for any properties that implements IDisposable as well, or am I doing this correctly?

like image 236
krillgar Avatar asked Jan 13 '14 18:01

krillgar


People also ask

How do you Dispose of IDisposable?

If you don't use using , then it's up to you (the calling code) to dispose of your object by explicitely calling Dispose().

What happens if you dont Dispose IDisposable?

IDisposable is usually used when a class has some expensive or unmanaged resources allocated which need to be released after their usage. Not disposing an object can lead to memory leaks.

What does the Dispose method do?

Dispose improves performance and optimizes memory by releasing unmanageable objects and scarce resources, like Graphics Device Interface (GDI) handles used in applications with restricted Windows space. The Dispose method, provided by the IDisposable interface, implements Dispose calls.

What does it mean when an object implements IDisposable?

Typically, types that use unmanaged resources implement the IDisposable or IAsyncDisposable interface to allow the unmanaged resources to be reclaimed. When you finish using an object that implements IDisposable, you call the object's Dispose or DisposeAsync implementation to explicitly perform cleanup.


3 Answers

Disposing an object doesn't automatically dispose all of its properties that are IDisposables. All it does is execute the Dispose method, what the object does then is up to the developer.

When an object of a class is disposed it should dispose all owned resources that implement the IDisposable interface. In your case you dispose an object that could very well be still in use.

I think the article from Stephen Cleary about IDisposable: What Your Mother Never Told You About Resource Deallocation explains about disposing objects and about the difficulties and problems that can arise in some circumstances.

like image 187
Dirk Avatar answered Oct 05 '22 21:10

Dirk


Dispose is never called automatically, not even by the garbage collector. If object A controls the lifecycle of object B, and if object B is disposable, object A should also be disposable and should manually call object B's Dispose method.

like image 23
Michael Gunter Avatar answered Oct 05 '22 21:10

Michael Gunter


No, it is not redundant. It is your job to implement IDisposable on any class that has disposable members.

Your check might be redundant. You should configure the mocking to allow calls to Dispose method on your repository object, even if you implement it as a "do nothing" method for mocking.

You should take a look at Disposable Pattern

It explain you how and when to implement IDisposable and also how the Destructor and Dispose methods should be linked.

like image 41
Cosmin Vană Avatar answered Oct 05 '22 22:10

Cosmin Vană