Uploaded image for project: 'Rules Repository'
  1. Rules Repository
  2. RSPEC-3234

"GC.SuppressFinalize" should not be invoked for types without destructors

    XMLWordPrintable

    Details

    • Type: Code Smell Detection
    • Status: Active
    • Resolution: Unresolved
    • Labels:
    • Message:
      Remove this useless call to "GC.SuppressFinalize".
    • Default Severity:
      Minor
    • Impact:
      Low
    • Likelihood:
      Low
    • Covered Languages:
      C#
    • Remediation Function:
      Constant/Issue
    • Constant Cost:
      2min
    • Analysis Scope:
      Main Sources, Test Sources
    • ReSharper:
      GCSuppressFinalizeForTypeWithoutDestructor
    • Quick-fixes:
      Hide

      Remove useless "SuppressFinalize" call: removes the useless call

      Show
      Remove useless "SuppressFinalize" call: removes the useless call

      Description

      GC.SuppressFinalize asks the Common Language Runtime not to call the finalizer of an object. This is useful when implementing the dispose pattern where object finalization is already handled in IDisposable.Dispose. However, it has no effect if there is no finalizer defined in the object's type, so using it in such cases is just confusing.

      This rule raises an issue when GC.SuppressFinalize is called for objects of sealed types without a finalizer.

      Note: S3971 is a stricter version of this rule. Typically it makes sense to activate only one of these 2 rules.

      Noncompliant Code Example

      sealed class MyClass
      {
        public void Method()
        {
          ...
          GC.SuppressFinalize(this); //Noncompliant
        }
      }
      

      Compliant Solution

      sealed class MyClass
      {
        public void Method()
        {
          ...
        }
      }
      

        Attachments

          Issue Links

            Activity

              People

              Assignee:
              Unassigned Unassigned
              Reporter:
              tamas.vajk Tamas Vajk (Inactive)
              Votes:
              0 Vote for this issue
              Watchers:
              1 Start watching this issue

                Dates

                Created:
                Updated: