Export (0) Print
Expand All
Expand Minimize

Call GC.SuppressFinalize correctly

TypeName

CallGCSuppressFinalizeCorrectly

CheckId

CA1816

Category

Microsoft. Usage

Breaking Change

Non Breaking

A method that is an implementation of Dispose does not call GC.SuppressFinalize.

- or -

A method that is not an implementation of Dispose calls GC.SuppressFinalize.

- or -

A method calls GC.SuppressFinalize and passes something other than this (Me in Visual Basic).

The Dispose method lets users release resources at any time before the object becoming available for garbage collection. If the Dispose method is called, it frees resources of the object. This makes finalization unnecessary. Dispose should call GC.SuppressFinalize so the garbage collector does not call the finalizer of the object.

To prevent derived types with finalizers from having to re-implement [System.IDisposable] and to call it, unsealed types without finalizers should still call GC.SuppressFinalize.

To fix a violation of this rule:

If the method is an implementation of Dispose, add a call to GC.SuppressFinalize.

If the method is not an implementation of Dispose, either remove the call to GC.SuppressFinalize or move it to the type's Dispose implementation.

Change all calls to GC.SuppressFinalize to pass this (Me in Visual Basic).

Only suppress a warning from this rule if you are deliberating using GC.SuppressFinalize to control the lifetime of other objects. Do not suppress a warning from this rule if an implementation of Dispose does not call GC.SuppressFinalize. In this situation, failing to suppress finalization degrades performance and provide no benefits.

The following example shows a method that incorrectly calls GC.SuppressFinalize by passing it true (True in Visual Basic).

using System;
using System.Data.SqlClient;
namespace Samples
{
    public class DatabaseConnector : IDisposable
    {
        private SqlConnection _Connection = new SqlConnection();

        public void Dispose()
        {
            Dispose(true);
            GC.SuppressFinalize(true);  // Violates rule
        }

        protected virtual void Dispose(bool disposing)
        {
            if (disposing)
            {
                if (_Connection != null)
                {
                    _Connection.Dispose();
                    _Connection = null;
                }
            }
        }
    }
}

The following example shows a method that correctly calls GC.SuppressFinalize.

using System;
using System.Data.SqlClient;

namespace Samples
{
    public class DatabaseConnector : IDisposable
    {
        private SqlConnection _Connection = new SqlConnection();

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

        protected virtual void Dispose(bool disposing)
        {
            if (disposing)
            {
                if (_Connection != null)
                {
                    _Connection.Dispose();
                    _Connection = null;
                }
            }
        }
    }
}

Community Additions

ADD
Show:
© 2014 Microsoft