[Mono-devel-list] [Patch] RReader not to rely on bounds checking

Willibald Krenn Willibald.Krenn at gmx.at
Sun Jan 30 14:45:37 EST 2005


>> 				StreamReader sr = new StreamReader (filename);
>> 				xml = sr.ReadToEnd ();
>> 				sr.Close ();
>>+				len = xml.Length;
> 
> 
> Isn't there a resource leak here if sr.ReadToEnd() fails? Wouldn't a
> "using" or "try/finally" construct be better here?

You mean


StreamReader sr = new StreamReader (filename);
try {
	xml = sr.ReadToEnd ();
} finally {
	sr.Close ();
}
len = xml.Length;


or even

using (StreamReader sr = new StreamReader (filename)) {
	xml = sr.ReadToEnd ();
}
len = xml.Length;

I agree, this should be used here..


> 
>> 			}
>> 			catch {
>> 				xml = null;
>>+				len = 0;
>>+				throw;
>> 			}
>> 		}
> 
> 
> What is the use of this catch block?

I really don't know. Probably this catch block has to clear the string 
so a subsequent call to Read fails (instead of possibly returning 
garbage/incomplete data)...

Willi




More information about the Mono-devel-list mailing list