update interprocedural examples (#114)

This commit is contained in:
Xiaoyu Liu 2022-03-08 22:38:45 +00:00 коммит произвёл GitHub
Родитель 6914801489
Коммит 2123394c1a
Не найден ключ, соответствующий данной подписи
Идентификатор ключа GPG: 4AEE18F83AFDEB23
1 изменённых файлов: 40 добавлений и 8 удалений

Просмотреть файл

@ -56,24 +56,56 @@ namespace Examples
// FIXME: should close the stream intraprocedurally by calling sw.Close() // 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> /// <summary>
/// Interprocedural resource usage example, leaks expected. /// Interprocedural resource usage example, leaks expected.
/// </summary> /// </summary>
public void ResourceLeakInterproceduralBad(){ public void ResourceLeakInterproceduralBad(){
SRGlobal = new StreamReader("whatever.txt"); StreamWriter stream = AllocateStreamWriter();
string data = SRGlobal.ReadToEnd(); if (stream == null)
Console.WriteLine(data); return;
// FIXME: should close the stream interprocedurally by calling Cleanup()
try
{
stream.WriteLine(12);
}
finally
{
// FIXME: should close the stream by calling stream.Close().
}
} }
/// <summary> /// <summary>
/// Interprocedural resource usage example, no leaks expected. /// Interprocedural resource usage example, no leaks expected.
/// </summary> /// </summary>
public void ResourceLeakInterproceduralOK(){ public void ResourceLeakInterproceduralOK(){
SRGlobal = new StreamReader("whatever.txt"); StreamWriter stream = AllocateStreamWriter();
string data = SRGlobal.ReadToEnd(); if (stream == null)
Console.WriteLine(data); return;
CleanUp();
try
{
stream.WriteLine(12);
}
finally
{
stream.Close();
}
} }
/// <summary> /// <summary>