Home All Groups Group Topic Archive Search About

XmlTextReader disposal pattern

Author
23 Oct 2006 12:21 AM
Leon
Hi,

I am amazed that XmlTextReader does not implement IDispoable. Is there any
reason?

In the case of XmlTextReader( Stream ) construction, if I wrap the Stream
with a C# using statement like this:
using( StringStream ss = new StringStream.... )
{
    XmlTextReader xr = new XmlTextReader( ss );
    // ... use xr
}

Will this upset the XmlTextReader's finalizer because that would be executed
non-deterministically long after ss has been disposed?

If inside the using() loop, I called XmlTextReader.Close(), will this upset
the ss.Dispose() call?

Thanks.

Leon

Author
23 Oct 2006 1:11 AM
Carl Daniel [VC++ MVP]
Leon wrote:
> Hi,
>
> I am amazed that XmlTextReader does not implement IDispoable. Is
> there any reason?

XmlTextReader does implement IDisposable (inherited from XmlReader) in .NET
2.0.  Apparently this was overlooked in 1.x.

> In the case of XmlTextReader( Stream ) construction, if I wrap the
> Stream with a C# using statement like this:
> using( StringStream ss = new StringStream.... )
> {
>    XmlTextReader xr = new XmlTextReader( ss );
>    // ... use xr
> }
>
> Will this upset the XmlTextReader's finalizer because that would be
> executed non-deterministically long after ss has been disposed?

No.  XmlTextReader in 1.0 doesn't have a finalizer (although the VS2003 docs
incorrectly state that it does).  In 2.0, the finalizer won't touch the
wrapped stream - it's too late at that point.

> If inside the using() loop, I called XmlTextReader.Close(), will this
> upset the ss.Dispose() call?

No, the stream will know that it's closed and won't do anything in Dispose.

-cd
Author
23 Oct 2006 1:18 AM
Leon
Thanks.

Another good reason to use .Net 2 ;-)

Leon

Show quote
"Carl Daniel [VC++ MVP]" wrote:

> Leon wrote:
> > Hi,
> >
> > I am amazed that XmlTextReader does not implement IDispoable. Is
> > there any reason?
>
> XmlTextReader does implement IDisposable (inherited from XmlReader) in .NET
> 2.0.  Apparently this was overlooked in 1.x.
>
> > In the case of XmlTextReader( Stream ) construction, if I wrap the
> > Stream with a C# using statement like this:
> > using( StringStream ss = new StringStream.... )
> > {
> >    XmlTextReader xr = new XmlTextReader( ss );
> >    // ... use xr
> > }
> >
> > Will this upset the XmlTextReader's finalizer because that would be
> > executed non-deterministically long after ss has been disposed?
>
> No.  XmlTextReader in 1.0 doesn't have a finalizer (although the VS2003 docs
> incorrectly state that it does).  In 2.0, the finalizer won't touch the
> wrapped stream - it's too late at that point.
>
> > If inside the using() loop, I called XmlTextReader.Close(), will this
> > upset the ss.Dispose() call?
>
> No, the stream will know that it's closed and won't do anything in Dispose.
>
> -cd
>
>
>

AddThis Social Bookmark Button