update interprocedural examples (#114)
This commit is contained in:
Родитель
6914801489
Коммит
2123394c1a
|
@ -56,24 +56,56 @@ namespace Examples
|
|||
// FIXME: should close the stream intraprocedurally by calling sw.Close()
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Returns a StreamWriter resource unless returns null with exception, no leaks expected.
|
||||
/// </summary>
|
||||
public StreamWriter AllocateStreamWriter()
|
||||
{
|
||||
try
|
||||
{
|
||||
FileStream fs = File.Create("everwhat.txt");
|
||||
return new StreamWriter(fs);
|
||||
}
|
||||
catch(Exception e)
|
||||
{
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Interprocedural resource usage example, leaks expected.
|
||||
/// </summary>
|
||||
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().
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Interprocedural resource usage example, no leaks expected.
|
||||
/// </summary>
|
||||
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();
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
|
|
Загрузка…
Ссылка в новой задаче