Gendarme.Rules.Performance.UseSuppressFinalizeOnIDisposableTypeWithFinalizerRule Class
This rule will fire if a type implements System.IDisposable and has a finalizer (called a destructor in C#), but the Dispose method does not call System.GC.SuppressFinalize. Failing to do this should not cause properly written code to fail, but it does place a non-trivial amount of extra pressure on the garbage collector and on the finalizer thread.

See Also: UseSuppressFinalizeOnIDisposableTypeWithFinalizerRule Members

Syntax

[Gendarme.Framework.EngineDependency(typeof(Gendarme.Framework.Engines.OpCodeEngine, Gendarme.Framework, Version=2.8.0.0, Culture=neutral, PublicKeyToken=null))]
[Gendarme.Framework.Problem("The type has a finalizer and implements IDisposable, but Dispose does not call System.GC.SuppressFinalize.")]
[Gendarme.Framework.Solution("Add a call to GC.SuppressFinalize inside your Dispose method.")]
public class UseSuppressFinalizeOnIDisposableTypeWithFinalizerRule : Gendarme.Framework.Rule, Gendarme.Framework.ITypeRule

Remarks

Prior to Gendarme 2.2 this rule was named IDisposableWithDestructorWithoutSuppressFinalizeRule

Example

Bad example:

Example

            class BadClass : IDisposable {
            	~BadClass ()
            	{
            		Dispose (false);
            	}
            	public void Dispose ()
            	{
            		// GC.SuppressFinalize is missing so the finalizer will be called
            		// which puts needless extra pressure on the garbage collector.
            		Dispose (true);
            	}
            	private void Dispose (bool disposing)
            	{
            		if (ptr != IntPtr.Zero) {
            			Free (ptr);
            			ptr = IntPtr.Zero;
            		}
            	}
            	[DllImport ("somelib")]
            	private static extern void Free (IntPtr ptr);
            	private IntPtr ptr;
            }
            

Example

Good example:

Example

            class GoodClass : IDisposable {
            	~GoodClass ()
            	{
            		Dispose (false);
            	}
            	public void Dispose ()
            	{
            		Dispose (true);
            		GC.SuppressFinalize (this);
            	}
            	private void Dispose (bool disposing)
            	{
            		if (ptr != IntPtr.Zero) {
            			Free (ptr);
            			ptr = IntPtr.Zero;
            		}
            	}
            	[DllImport ("somelib")]
            	private static extern void Free (IntPtr ptr);
            	private IntPtr ptr;
            }
            

Requirements

Namespace: Gendarme.Rules.Performance
Assembly: Gendarme.Rules.Performance (in Gendarme.Rules.Performance.dll)
Assembly Versions: 2.8.0.0