F
Fernando Cacciola
Hi People,
Consider the following:
class A : IDisposable
{
public A ( Stream aResource ) { mResource = aResource ; }
public void Dispose()
{
if ( !mDisposed )
{
Dispose(true);
GC.SuppressFinalize(this);
}
}
protected virtual void Dispose( bool aDisposing )
{
if ( aDisposing )
{
mResource.Dispose();
}
mDisposed = true ;
}
~A() { Dispose(false); }
bool mDisposed = false ;
Stream mResource = null ;
}
AFAICT I can only call mResource.Dispose() in the "Disposing" branch.
That means that for this particular class, its finalizer won't (and can't)
do anything.
So, is it correct to say that this class needs Dispose but not the
finalizer?
Or more generally, are the following guidelines correct?
* Implement a finalizer if the class itself has unmanaged resources
* Implement Dispose if the class OR ANY DEPENDENT OBJECTS has unmanaged
resources.
* Use the Dispose pattern ONLY if you need to implement BOTH (don't use it
if you just need Dispose)
If the above is correct, the following is the correct implementation for
Dispose in this class:
class A : IDisposable
{
public A ( Stream aResource ) { mResource = aResource ; }
public void Dispose()
{
if ( !mDisposed )
{
mResource.Dispose();
mDisposed = true ;
}
}
bool mDisposed = false ;
Stream mResource = null ;
}
Some readers may not know this but finalizers are expensive (their force the
object to live up to the last GC generation and even then are totally free
only after an _additonal_ GC collection). You don't want to add one unless
strictly required.
TIA
Fernando Cacciola
Consider the following:
class A : IDisposable
{
public A ( Stream aResource ) { mResource = aResource ; }
public void Dispose()
{
if ( !mDisposed )
{
Dispose(true);
GC.SuppressFinalize(this);
}
}
protected virtual void Dispose( bool aDisposing )
{
if ( aDisposing )
{
mResource.Dispose();
}
mDisposed = true ;
}
~A() { Dispose(false); }
bool mDisposed = false ;
Stream mResource = null ;
}
AFAICT I can only call mResource.Dispose() in the "Disposing" branch.
That means that for this particular class, its finalizer won't (and can't)
do anything.
So, is it correct to say that this class needs Dispose but not the
finalizer?
Or more generally, are the following guidelines correct?
* Implement a finalizer if the class itself has unmanaged resources
* Implement Dispose if the class OR ANY DEPENDENT OBJECTS has unmanaged
resources.
* Use the Dispose pattern ONLY if you need to implement BOTH (don't use it
if you just need Dispose)
If the above is correct, the following is the correct implementation for
Dispose in this class:
class A : IDisposable
{
public A ( Stream aResource ) { mResource = aResource ; }
public void Dispose()
{
if ( !mDisposed )
{
mResource.Dispose();
mDisposed = true ;
}
}
bool mDisposed = false ;
Stream mResource = null ;
}
Some readers may not know this but finalizers are expensive (their force the
object to live up to the last GC generation and even then are totally free
only after an _additonal_ GC collection). You don't want to add one unless
strictly required.
TIA
Fernando Cacciola