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()
|
// 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>
|
||||||
|
|
Загрузка…
Ссылка в новой задаче