7

I have the following method GetData that creates a StreamReader from a file.

private void GetData(string file)
{
    string filename = Path.GetFileNameWithoutExtension(file);
    XmlDocument xmldoc = new XmlDocument();

    using (StreamReader sr = new StreamReader(file))
    {
        Stream bs = sr.BaseStream;
        Stream cl = mainParser.CleanMarkup(bs);
        try
        {
            xmldoc = mainParser.LoadDocument(bs);
        }
        catch (XmlException ex)
        {
            // Exceptions are usually caused by non-compliant documents.
            // These errors are not critical to the operation of this program.
            Console.WriteLine(filename + " " + ex.Message);
        }
    }
    Msdn msdnParser = new Msdn(xmldoc);

    ListViewItem lvitem = new ListViewItem(filename);
    lvitem.SubItems.Add(filename);
    foreach (string item in msdnParser.Subitems)
    {
        lvitem.SubItems.Add(item);
    }
    listView.Items.Add(lvitem);
}

mainParser.LoadDocument(bs) calls the following:

public XmlDocument LoadDocument(Stream file)
{
    XmlDocument xmldoc = new XmlDocument();
    XmlReader xmlread = XmlReader.Create(file);
    xmldoc.Load(xmlread);

    return xmldoc;
}

StreamReader is disposed by GetData. Does this mean that I don't have to dispose of XmlReader since (I believe) this would dispose of its only unmanaged resource?

Leonard Thieu
  • 785
  • 1
  • 7
  • 21

2 Answers2

8

The best "rule of thumb" to work by is:

If something implements IDisposable, always wrap it in a using() block to ensure that any unmanaged resources it owns are disposed of correctly.

Relying on the fact that the current implementation of "something" disposes of an underlying resource is dangerous and it won't hurt to wrap everything in a using, just to be on the safe side =)

Rob
  • 45,296
  • 24
  • 122
  • 150
6

You're right, you don't have to dispose the reader. But in the code given, it wouldn't hurt either.

I would not put a using block inside LoadDocument() because it is designed so that it 'borrows' it's stream (it does not create it).

But there are arguments to Dispose the XmlReader anyway, just because it's IDisposable. I don't think there is a clear winner here because of the disputable design of the Reader (and Writer) family: They Dispose their baseStreams without clearly being the owner of those streams.

H H
  • 263,252
  • 30
  • 330
  • 514
  • 1
    Based off of what I read, `XmlReaderSettings` has a property `CloseInput` that defaults to `true`. Should I consider disabling this default behavior so I can dispose of `XmlReader` without disposing the underlying `Stream`? – Leonard Thieu Aug 18 '10 at 23:53
  • 1
    My book lied to me. The default value for `CloseInput` is `false`. – Leonard Thieu Aug 19 '10 at 03:35