title: Passing IDisposable objects through constructor chains
body: <p>I've got a small hierarchy of objects that in general gets constructed from data in a <code>Stream</code>, but for some particular subclasses, can be synthesized from a simpler argument list. In chaining the constructors from the subclasses, I'm running into an issue with ensuring the disposal of the synthesized stream that the base class constructor needs. Its not escaped me that the use of <code>IDisposable</code> objects this way is possibly just dirty pool (plz advise?) for reasons I've not considered, but, this issue aside, it seems fairly straightforward (and good encapsulation).</p>
  
  <p>Codes:</p>
  
  <pre><code>abstract class Node {
      protected Node (Stream raw)
      {
          // calculate/generate some base class properties
      }
  }
  class FilesystemNode : Node {
      public FilesystemNode (FileStream fs)
          : base (fs)
      {
          // all good here; disposing of fs not our responsibility
      }
  }
  class CompositeNode : Node {
      public CompositeNode (IEnumerable some_stuff)
          : base (GenerateRaw (some_stuff))
      {
          // rogue stream from GenerateRaw now loose in the wild!
      }
  
      static Stream GenerateRaw (IEnumerable some_stuff)
      {
          var content = new MemoryStream ();
          // molest elements of some_stuff into proper format, write to stream
          content.Seek (0, SeekOrigin.Begin);
          return content;
      }
  }
  </code></pre>
  
  <p>I realize that not disposing of a <code>MemoryStream</code> is not exactly a world-stopping case of bad CLR citizenship, but it still gives me the heebie-jeebies (not to mention that I may not always be using a <code>MemoryStream</code> for other subtypes). It's not in scope, so I can't explicitly <code>Dispose ()</code> it later in the constructor, and adding a <code>using</code> statement in <code>GenerateRaw ()</code> is self-defeating since I need the stream returned.</p>
  
  <p>Is there a better way to do this?</p>
  
  <p>Preemptive strikes:</p>
  
  <ul>
  <li>yes, the properties calculated in the <code>Node</code> constructor should be part of the base class, and should not be calculated by (or accessible in) the subclasses</li>
  <li>I won't require that a stream be passed into CompositeNode (its format should be irrelevant to the caller)</li>
  <li>The previous iteration had the value calculation in the base class as a separate protected method, which I then just called at the end of each subtype constructor, moved the body of <code>GenerateRaw ()</code> into a using statement in the body of the <code>CompositeNode</code> constructor. But the repetition of requiring that call for each constructor and not being able to guarantee that it be run for every subtype ever (a <code>Node</code> is not a <code>Node</code>, semantically, without these properties initialized) gave me heebie-jeebies far worse than the (potential) resource leak here does.</li>
  </ul>