G
Guest
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.
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.