RE: Code Analysis - CA1816 (CallGCSuppressFinalize)

Tech Tip: Click here to run a free scan for Windows Errors and optimize PC performance



A disposable type needs to implement IDisposable & provide a public
Dispose(void) method that ends the object’s lifetime. If the type is not
sealed, it should provide a protected Dispose(bool disposing) method where
the actual cleanup logic lives. Dispose(void) then calls Dispose(true)
followed by GC.SuppressFinalize(this). If your object needs a finalizer,
then the finalizer calls Dispose(false). The cleanup logic in Dispose(bool)
needs to be written to run correctly when called explicitly from
Dispose(void), as well as from a finalizer thread. Dispose(void) and
Dispose(bool) should be safely runnable multiple times, with no ill effects

//the dispose pattern
public void Dispose()
{
Dispose(true);
GC.SuppressFinalize(this);
}

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


--
Misbah Arefin
https://mcp.support.microsoft.com/profile/MISBAH.AREFIN
http://www.linkedin.com/in/misbaharefin


"NvrBst" wrote:

Code Analysis always returns the following message on a bunch of my
classes.

Warning 75 CA1816 : Microsoft.Usage : Change 'xxx.Dispose()' to call
'GC.SuppressFinalize(object)'. This will prevent unnecessary
finalization of the object once it has been disposed and it has fallen
out of scope.


None of my classes have a finalizer, they all simply have the
following (or very simular to the following)
----------
private bool disposed = false;
public void Dispose() { Dispose(true); }
protected virtual void Dispose(bool disposing) {
if(!disposed) {
if(disposing && slimLock != null) slimLock.Dispose();
disposed = true;
}
}
----------

Joe Duffy's Weblog (http://www.bluebytesoftware.com/blog/2005/04/08/
DGUpdateDisposeFinalizationAndResourceManagement.aspx)
--Says not to call Dispose if my class doesn't have the "~MyClass()"
finalizer method.

MSDN (http://msdn2.microsoft.com/en-us/library/ms182269.aspx)
--Says, basically, don't ignore this warning and to add the code.


Should I be calling the SupressFinalize anyway (IE doesn't hurt
anything performance wise), or just ignore the Code Analysis warning?
Or 'something strange with my code because I dont get the warning'
kind of thing?

Thanks :) NB

.



Relevant Pages

  • Re: StatusStrip control leaks its collection items
    ... you'd followed the .NET requirement of disposing disposable objects, ... But I still maintain that an object that wraps an unmanaged resource ... must release that resource in its finalizer. ... Now back to the problem at hand, I am finding that controls removed ...
    (microsoft.public.dotnet.framework)
  • Re: About speed
    ... IDisposable needs a finalizer, and most code that I see that implements ... void IDisposable.Dispose ... protected virtual void Dispose(bool disposing) ... then disposed explicitly (using IDispose), ...
    (borland.public.delphi.non-technical)
  • Re: About speed
    ... "disposing" is true iff Disposewas called explicitly via ... Dispose, Close, IDisposable.Disposeor some other explicit ... Being called by the finalizer implies being called on the ...
    (borland.public.delphi.non-technical)
  • Re: Can lock into a dispose?
    ... And Dispose of components shouldn't be called by the Finalizer ... wich has to occur during disposing aswell as during ... But either disposing is true or false, I would serialize the class, ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: Accessing private data during finalize/dispose
    ... > I understand the basics of finalization and implementing IDisposable and how one is guaranteed access to managed objects only when working through the IDisposable interface. ... class A provides management for the creation ... > protected Dispose(bool disposing) ... You can safely access other objects from a finalizer, ...
    (microsoft.public.dotnet.languages.csharp)