Always close script linemap file after reading and always dispose of other streams in the script engine even if exceptions are thrown.

mb-throttle-test
Justin Clark-Casey (justincc) 2014-12-03 18:58:55 +00:00
parent caa7b1e6a1
commit 72d1d96c5c
2 changed files with 29 additions and 29 deletions

View File

@ -663,9 +663,8 @@ namespace SecondLife
try try
{ {
FileStream fs = File.Open(assembly, FileMode.Open, FileAccess.Read); using (FileStream fs = File.Open(assembly, FileMode.Open, FileAccess.Read))
fs.Read(data, 0, data.Length); fs.Read(data, 0, data.Length);
fs.Close();
} }
catch (Exception) catch (Exception)
{ {
@ -680,9 +679,8 @@ namespace SecondLife
Byte[] buf = Encoding.ASCII.GetBytes(filetext); Byte[] buf = Encoding.ASCII.GetBytes(filetext);
FileStream sfs = File.Create(assembly + ".text"); using (FileStream sfs = File.Create(assembly + ".text"))
sfs.Write(buf, 0, buf.Length); sfs.Write(buf, 0, buf.Length);
sfs.Close();
return assembly; return assembly;
} }
@ -775,7 +773,6 @@ namespace SecondLife
return message; return message;
} }
private static void WriteMapFile(string filename, Dictionary<KeyValuePair<int, int>, KeyValuePair<int, int>> linemap) private static void WriteMapFile(string filename, Dictionary<KeyValuePair<int, int>, KeyValuePair<int, int>> linemap)
{ {
string mapstring = String.Empty; string mapstring = String.Empty;
@ -787,40 +784,42 @@ namespace SecondLife
} }
Byte[] mapbytes = Encoding.ASCII.GetBytes(mapstring); Byte[] mapbytes = Encoding.ASCII.GetBytes(mapstring);
FileStream mfs = File.Create(filename);
mfs.Write(mapbytes, 0, mapbytes.Length);
mfs.Close();
}
using (FileStream mfs = File.Create(filename))
mfs.Write(mapbytes, 0, mapbytes.Length);
}
private static Dictionary<KeyValuePair<int, int>, KeyValuePair<int, int>> ReadMapFile(string filename) private static Dictionary<KeyValuePair<int, int>, KeyValuePair<int, int>> ReadMapFile(string filename)
{ {
Dictionary<KeyValuePair<int, int>, KeyValuePair<int, int>> linemap; Dictionary<KeyValuePair<int, int>, KeyValuePair<int, int>> linemap;
try try
{ {
StreamReader r = File.OpenText(filename); using (StreamReader r = File.OpenText(filename))
linemap = new Dictionary<KeyValuePair<int, int>, KeyValuePair<int, int>>();
string line;
while ((line = r.ReadLine()) != null)
{ {
String[] parts = line.Split(new Char[] { ',' }); linemap = new Dictionary<KeyValuePair<int, int>, KeyValuePair<int, int>>();
int kk = System.Convert.ToInt32(parts[0]);
int kv = System.Convert.ToInt32(parts[1]);
int vk = System.Convert.ToInt32(parts[2]);
int vv = System.Convert.ToInt32(parts[3]);
KeyValuePair<int, int> k = new KeyValuePair<int, int>(kk, kv); string line;
KeyValuePair<int, int> v = new KeyValuePair<int, int>(vk, vv); while ((line = r.ReadLine()) != null)
{
String[] parts = line.Split(new Char[] { ',' });
int kk = System.Convert.ToInt32(parts[0]);
int kv = System.Convert.ToInt32(parts[1]);
int vk = System.Convert.ToInt32(parts[2]);
int vv = System.Convert.ToInt32(parts[3]);
linemap[k] = v; KeyValuePair<int, int> k = new KeyValuePair<int, int>(kk, kv);
KeyValuePair<int, int> v = new KeyValuePair<int, int>(vk, vv);
linemap[k] = v;
}
} }
} }
catch catch
{ {
linemap = new Dictionary<KeyValuePair<int, int>, KeyValuePair<int, int>>(); linemap = new Dictionary<KeyValuePair<int, int>, KeyValuePair<int, int>>();
} }
return linemap; return linemap;
} }
} }
} }

View File

@ -1065,10 +1065,11 @@ namespace OpenSim.Region.ScriptEngine.Shared.Instance
{ {
try try
{ {
FileStream fs = File.Create(Path.Combine(Path.GetDirectoryName(assembly), ItemID.ToString() + ".state")); using (FileStream fs = File.Create(Path.Combine(Path.GetDirectoryName(assembly), ItemID.ToString() + ".state")))
Byte[] buf = Util.UTF8NoBomEncoding.GetBytes(xml); {
fs.Write(buf, 0, buf.Length); Byte[] buf = Util.UTF8NoBomEncoding.GetBytes(xml);
fs.Close(); fs.Write(buf, 0, buf.Length);
}
} }
catch(Exception) catch(Exception)
{ {