title: Is PdfStamper disposing output stream? (iTextSharp)
body: <p>I am using iTextSharp to add page numbers to a PDF with C#. While running code analysis the <code>MemoryStream</code> for the output is suspected to be disposed more than once. <a href="https://msdn.microsoft.com/query/dev12.query?appId=Dev12IDEF1&amp;l=EN-US&amp;k=k(CA2202);k(TargetFrameworkMoniker-.NETFramework,Version%3Dv4.5)&amp;rd=true" rel="nofollow">See this warning generated by Visual Studio.</a> Is this an API problem? Should the second parameter of <code>PdfStamper</code> be marked as <code>out</code>? Is there a way for me to fix this warning?</p>
  
  <pre><code>MemoryStream mem = null;
  PdfReader reader = null;
  PdfStamper stamper = null;
  try
  {
      mem = new MemoryStream();
      reader = new PdfReader(m_pdf);                
      stamper = new PdfStamper(reader, mem);
  
      // do stuff
      stamper.Close();
      var result = mem.ToArray();
  }
  finally
  {
      if(stamper != null)
      {
          stamper.Dispose();
      }
  
      if (reader != null)
      {
          reader.Dispose();
      }
  
      if (mem != null)
      {
          mem.Dispose();
      }
  }
  </code></pre>