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()
}
/// <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>