nickdu
Tue Nov 29 07:59:18 CST 2005
You wrote:
"The reason BinaryWriter class does not have a finalizer is that it has a
reference to the filestream object and there can be no guarantee of the
order the objects are finalized."
Is this the case? I would think:
The reason why the BinaryWriter does not have a finalizer is because it
doesn't need one. It does not *directly* (as Brian suggests) hold onto any
unmanaged resources. It has nothing to do with the order, or lack there of,
the objects are finalized.
Are you suggesting that if the order of finalization was deterministic, and
in this case the FileStream being finalized after the BinaryWriter, that the
BinaryWriter should contain a finalizer so that it could call Flush()?
You also stated:
"Other than these dependencies cases, the only other place where Dispose
exists, but no finalizer is the Component (System.ComponentModel) class.
This is more because the object lifetimes should be controlled by its
Container."
This makes it sound like the typical case is having a finalizer when
implementing IDisposable. I would think the opposite is true. I would think
the more common case are classes that need to implement IDisposable (to
provide explicit cleanup) but which do not directly hold onto unmanaged
resources and thus don't require a finalizer.
--
Thanks,
Nick
"Manoj G [MVP]" wrote:
> Some thoughts...
>
> "nickdu" <nickdu@discussions.microsoft.com> wrote in message
> news:689E6950-4378-4AAE-911D-CDB64CB38F59@microsoft.com...
> >I believe that I fully understand the concepts behind IDisposable and
> > finalization (at least I hope I do) and I would like to get feedback from
> > the
> > experts so that I can forward them on to others.
> >
> > There are people who think that if a class implements IDisposable then it
> > should include a finalizer.
>
> I think it is the other way around more rightly - classes that have a
> finalizer implement IDisposable.
> and the Dispose method supresses the finalization after cleaning up the
> resources.
>
> >What I told them was that you should only
> > include a finalizer if you have unmanaged resources you need to ensure are
> > cleaned up.
>
> Right.
>
> >Otherwise you should not include a finalizer whether or not you
> > implement IDisposable.
> > As an example of this case, the one where you might want to implement
> > IDisposable but do not need a finalizer, I said assume you have a class e
> > that
> > internally makes use of a FileStream object. It might open the object up
> > at
> > some point during its lifetime and keep it open from then on. In this
> > case
> > you might want to implement IDisposable to provide an explicit mechanism
> > for
> > the caller to indicate they are done with your class. The implementation
> > in
> > your class would simply call the FileStream's IDisposable.Dispose()
> > method.
> >
> > I argue that this class does not need a finalizer and in fact if this
> > class
> > did include a finalizer it would be of no use. By the time the finalizer
> > was
> > called you could not do anything with your FileStream instance as it may
> > have
> > already been disposed (assuming you don't expose the instance outside your
> > class).
> >
>
> A better explanation can be justified taking the example of the BinaryWriter
> class. The reason BinaryWriter class does
> not have a finalizer is that it has a reference to the filestream object and
> there can be no guarantee of the order
> the objects are finalized. If the filestream object is finalized first, the
> binarywriter would not have the stream to flush the remaining bytes
> in the buffer whenever it would be finalized itself.
>
> Hence, the BinaryWriter demands deterministic release of resource through
> dispose pattern.
>
> Other than these dependencies cases, the only other place where Dispose
> exists, but no finalizer is the Component (System.ComponentModel) class.
> This is more because the object lifetimes should be controlled by its
> Container.
>
> > Is my statement/explanations valid?
> > --
> > Thanks,
> > Nick
>
>
> --
> HTH,
> Manoj G
> MVP, Visual Developer
>
http://msmvps.com/manoj
>
>
>