Re: SuppressFinalize in Dispose confusion

Tech-Archive recommends: Fix windows errors by optimizing your registry



To clarify one point:

1. You need to implement IDisposable because the only thing that will call
Dispose() on your socket is your class. If you dont implement it or nobody
calls it then the socket will not be closed until the GC runs its finalizer
which is usually later than you would want.

2. You don't need to implement a finalizer because the GC will call the
finalizer of the socket when it collects it.
In fact even if you had a finalizer it should not touch the socket because
the GC can collect and finalize the socket BEFORE finalizing the object that
references it.

3. You suppress finalization after disposing as an optimization because
there is an associated overhead (it keeps the object around longer than
necessary)

"uncaged" <uncaged@xxxxxxxxxxxxxxxxxxxxxxxxx> wrote in message
news:9248E879-BBE5-46A2-AA75-CADCD7616620@xxxxxxxxxxxxxxxx
I'm confused about using SuppressFinalize in Dispose.

My class has a Socket member, so FxCop complained that my class needs to
implement IDispose.

Documentation says that either Dispose or the finalizer (destructor?) get
called, but never both. Is that because the examples have
SuppressFinalize
being called in Dispose?

In my attempt to follow the examples, I've got a destructor that calls
Dispose(false), a Dispose() that calls Dispose(true) and
GC.SupressFinalize(this), and a Dispose(bool) that looks like:

protected virtual void Dispose( Boolean disposing)
{
if (! disposed)
{
if (disposing)
{
if (listenerSocket != null)
{
listenerSocket.Close();
listenerSocket = null;
}
}
disposed = true;
}
}

First of all, is this right?

Next, I'm confused about SuppressFinalize being called in Dispose(). My
class has other object members. Does my Dispose(bool) need to set them
all
to null in order for them to get cleaned up by garbage collection? If
not,
when are they available for collection?

I'm also confused about the examples. In the SuppressFinalize
documentation
(ms-help://MS.VSCC.v80/MS.MSDN.v80/MS.NETDEVFX.v20.en/cpref2/html/M_System_GC_SuppressFinalize_1_b4c5a2da.htm),
for example, in the Dispose(bool disposing) function, it says to dispose
of
managed resources only if disposing is true, but to dispose of unmanaged
resource regardless of the value of disposing. Socket is managed, but
isn't
it IDisposable because it manipulates an unmanaged resource? So should I
call Socket's Close only if disposing is true, or regardless? By the way,
I'm supposed to call Close, right? I thought I was supposed to call
Socket's
Dispose, but that's protected.

Thanks.



.



Relevant Pages

  • Re: "using" vs "= null" and object lifetime
    ... IDispose, it doesn't mean that the object doesn't need to be GCed anymore. ... finalizer. ... is meant to be a safeguard if people don't call Dispose. ... The process of having the memory reclaimed ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: About speed
    ... IDisposable needs a finalizer, and most code that I see that implements ... void IDisposable.Dispose ... call GC.SupressFinalizer inside their Dispose method. ... then disposed explicitly (using IDispose), ...
    (borland.public.delphi.non-technical)
  • Re: Question on implementing IDisposable
    ... have dealth with the unmanaged resources and before you take your ... This class does not implement a finalizer ... The overloaded Dispose which takes a bool argument should be made ... Then, the base class can implement IDispose, so ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: Finalization and .NET/deterministic or orderly shutdown
    ... > production worthy application really call dispose and implement a ... references to that class to use the IDispose. ... then at the very least it's in an AppDomain shutdown. ... >> A finalizer is there for objects that you just leave hanging in the air ...
    (microsoft.public.dotnet.framework)
  • Re: How do I kill or break out of a synchronous Socket.Accept() ca
    ... I've tried calling both Closeand/or Shutdown(). ... Socket doesn't have a Dispose(), or I would have tried that too. ...
    (microsoft.public.dotnet.general)