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).

Imports System
Imports System.Data.SqlClient

Namespace Samples

    Public Class DatabaseConnector
        Implements IDisposable

        Private _Connection As New SqlConnection

        Public Sub Dispose() Implements IDisposable.Dispose
            Dispose(True)
            GC.SuppressFinalize(True)   ' Violates rules
        End Sub 

        Protected Overridable Sub Dispose(ByVal disposing As Boolean)
            If disposing Then 
                If _Connection IsNot Nothing Then
                    _Connection.Dispose()
                    _Connection = Nothing 
                End If 
            End If 
        End Sub 

    End Class 

End Namespace

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

Imports System
Imports System.Data.SqlClient

Namespace Samples

    Public Class DatabaseConnector
        Implements IDisposable

        Private _Connection As New SqlConnection

        Public Sub Dispose() Implements IDisposable.Dispose
            Dispose(True)
            GC.SuppressFinalize(Me)
        End Sub 

        Protected Overridable Sub Dispose(ByVal disposing As Boolean)
            If disposing Then 
                If _Connection IsNot Nothing Then
                    _Connection.Dispose()
                    _Connection = Nothing 
                End If 
            End If 
        End Sub 

    End Class 

End Namespace

Community Additions

ADD
Show:
© 2014 Microsoft