*Dependent* Unmanaged Resources, Dispose and finalizers

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
 
R

Richard Blewett [DevelopMentor]

Yes, you do not need a finalizer unless you *directly* control non-managed resources. In fact not only do you not need one *you should not implement one*

Finalizers not only cause the object to live longer they also make it more expensive to allocate as the allocation code path for a finalizable object is different than for an object with no finalizer.

Regards

Richard Blewett - DevelopMentor
http://www.dotnetconsult.co.uk/weblog
http://www.dotnetconsult.co.uk

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



[microsoft.public.dotnet.languages.csharp]
 
F

Fernando Cacciola

Richard Blewett said:
Yes, you do not need a finalizer unless you *directly* control non-managed
resources. In fact not only do you not need one *you should not implement
one*

Finalizers not only cause the object to live longer they also make it more
expensive to allocate as the allocation code path for a finalizable object
is different than for an object with no finalizer.

Regards

Richard Blewett - DevelopMentor
http://www.dotnetconsult.co.uk/weblog
http://www.dotnetconsult.co.uk
Thanks for the response.
And great blog! :)

Fernando Cacciola
SciSoft
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top