XmlTextReader disposal pattern

G

Guest

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
 
C

Carl Daniel [VC++ MVP]

Leon said:
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
 

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