diff --git a/Examples/Examples/Program.cs b/Examples/Examples/Program.cs index 65324cd..834798b 100644 --- a/Examples/Examples/Program.cs +++ b/Examples/Examples/Program.cs @@ -56,24 +56,56 @@ namespace Examples // FIXME: should close the stream intraprocedurally by calling sw.Close() } + /// + /// Returns a StreamWriter resource unless returns null with exception, no leaks expected. + /// + public StreamWriter AllocateStreamWriter() + { + try + { + FileStream fs = File.Create("everwhat.txt"); + return new StreamWriter(fs); + } + catch(Exception e) + { + return null; + } + } + /// /// Interprocedural resource usage example, leaks expected. /// public void ResourceLeakInterproceduralBad(){ - SRGlobal = new StreamReader("whatever.txt"); - string data = SRGlobal.ReadToEnd(); - Console.WriteLine(data); - // FIXME: should close the stream interprocedurally by calling Cleanup() + StreamWriter stream = AllocateStreamWriter(); + if (stream == null) + return; + + try + { + stream.WriteLine(12); + } + finally + { + // FIXME: should close the stream by calling stream.Close(). + } } /// /// Interprocedural resource usage example, no leaks expected. /// public void ResourceLeakInterproceduralOK(){ - SRGlobal = new StreamReader("whatever.txt"); - string data = SRGlobal.ReadToEnd(); - Console.WriteLine(data); - CleanUp(); + StreamWriter stream = AllocateStreamWriter(); + if (stream == null) + return; + + try + { + stream.WriteLine(12); + } + finally + { + stream.Close(); + } } ///