CA1816: Call GC.SuppressFinalize correctly

 

The new home for Visual Studio documentation is Visual Studio 2017 Documentation on docs.microsoft.com.

The latest version of this topic can be found at CA1816: Call GC.SuppressFinalize correctly.

TypeName|CallGCSuppressFinalizeCorrectly|
|CheckId|CA1816|
|Category|Microsoft. Usage|
|Breaking Change|Non Breaking|

The IDisposable.Dispose method lets users release resources at any time before the object becoming available for garbage collection. If the IDisposable.Dispose method is called, it frees resources of the object. This makes finalization unnecessary. IDisposable.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.

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;
                }
            }
        }
    }
}

CA2215: Dispose methods should call base class dispose

CA2216: Disposable types should declare finalizer

Dispose Pattern

Show: