Skip to content

Gendarme.Rules.Correctness.DisposableFieldsShouldBeDisposedRule(2.10)

Sebastien Pouliot edited this page Feb 9, 2011 · 3 revisions

DisposableFieldsShouldBeDisposedRule

Assembly: Gendarme.Rules.Correctness
Version: 2.10

Description

The rule inspects all fields for disposable types and, if System.IDisposable is implemented, checks that the type's Dispose method does indeed call Dispose on all disposable fields.

Examples

Bad example:

class DoesNotDisposeMember : IDisposable {
    byte[] buffer;
    IDisposable field;
    public void Dispose ()
    {
        buffer = null;
        // field is not disposed
    }
}

Good example:

class DisposePattern : IDisposable {
    byte[] buffer;
    IDisposable field;
    bool disposed;
    public void Dispose ()
    {
        Dispose (true);
    }
    private void Dispose (bool disposing)
    {
        if (!disposed) {
            if (disposing) {
                field.Dispose ();
            }
            buffer = null;
            disposed = true;
        }
    }
}
Clone this wiki locally