Re: SuppressFinalize in Dispose confusion
- From: "Nick Hounsome" <nh002@xxxxxxxxxxxxxxxxxx>
- Date: Sat, 18 Mar 2006 08:49:21 GMT
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.
.
- Follow-Ups:
- Re: SuppressFinalize in Dispose confusion
- From: Jon Skeet [C# MVP]
- Re: SuppressFinalize in Dispose confusion
- Prev by Date: Re: Converting to big -endian
- Next by Date: Re: Converting to big -endian
- Previous by thread: RE: SuppressFinalize in Dispose confusion
- Next by thread: Re: SuppressFinalize in Dispose confusion
- Index(es):
Relevant Pages
|