Question

I have been looking at the standard Dispose pattern and I'm just wondering what I need to write to free managed resources? If these resources are 'managed' already then surely I shouldn't need to do anything.

If that's the case, and my class doesn't hold any unmanaged resources (hence no need for it to be finalized by GC) then do I only need to suppress finalization in my Dispose method? :-

public void Dispose()
{
   GC.SuppressFinalize(this);
}

so suppose this is my class:

public sealed class MyClass : IDisposable
{
    IList<MyObject> objects; // MyObject doesn't hold any unmanaged resource
    private bool _disposed;

    public void Dispose()
    {
        Dispose(true);
        GC.SuppressFinalize(this);
    }

    private void Dispose(bool disposing)
    {
        if (!_disposed)
        {  
            // do I need to set the list to null and 
            // call Dispose on each item in the list?
            if (disposing)
            {
                foreach (var o in objects)
                    o.Dispose();

                objects = null;
            }
        }

        _disposed = true;
    }

    ~MyClass()
    {
        Dispose(false);
    }
}

Do I actually need to free the managed resources here?

Thanks,

Was it helpful?

Solution

If your class holds any IDisposable instances, you're using managed resources and thus you should implement IDisposable to allow users to dispose of the resources. Your Dispose method should call Dispose on the managed resources.

As for freeing managed memory, you don't need to do anything. That is handled by GC, but that is the only part of the cleanup that is handled by GC. Managed and unmanaged resources must be cleaned up by Dispose and/or finalizers.

If you don't use any managed or unmanaged resources, there is no need to implement neither IDisposable nor a finalizer. Implementing a finalizer will actually impact the performance of your type, so don't implement it unless you need it.

OTHER TIPS

You should dispose any of the managed objects that implement IDisposable.

You won't be able to call Dispose on objects that don't implement IDisposable so you'll need to check for that. (Obviously, if all possible instances/descendants of MyObject will always implement IDisposable then you won't need that check.)

There's no need to set the list itself to null.

In the general case, I'd probably re-write the loop to look something like this:

if (disposing)
{
    foreach (var o in objects)
    {
        var d = o as IDisposable;
        if (d != null) d.Dispose();
    }
}

(By the way, if your class doesn't actually hold any IDisposable objects or unmanaged resources then you probably don't need to implement IDisposable, or a finaliser, at all.)

There are two reasons to implement IDisposable:
1. To free UN-managed resources. This case is VERY rare - but unfortunately its what a LOT of the doc talks about. This is about WHAT to free - i.e avoiding leaked resources/memory.
2. To free MANAGED resource. This is common - it is NOT about ensuring what gets freed (because managed resources will always get freed at some point by the GC) but about WHEN things get freed. I.e its about giving the user of your object control over WHEN it releases managed resources (i.e when it closes sockets or files etc) so other things can get at them.

In your case you can get away with the following minimum dispose provided no derived class adds managed resources:

public virtual void Dispose() {
    foreach (var o in objects) {
        var d = o as IDisposable;
        if (d != null) d.Dispose();
    }
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top