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

Re: XmlTextReader disposal pattern by Carl

Carl
Sun Oct 22 20:11:02 CDT 2006

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



Re: XmlTextReader disposal pattern by newsgroup01

newsgroup01
Sun Oct 22 20:18:01 CDT 2006

Thanks.

Another good reason to use .Net 2 ;-)

Leon

"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
>
>
>