From e41e52e09727842d990a31e2a5f7f3e9c88fe8b3 Mon Sep 17 00:00:00 2001 From: Oren Hurvitz Date: Sun, 1 Jun 2014 17:39:11 +0300 Subject: Close streams immediately when we finish using them --- .../Framework/Scenes/Animation/BinBVHAnimation.cs | 49 ++++++++++--------- OpenSim/Region/Framework/Scenes/KeyframeMotion.cs | 46 +++++++++-------- OpenSim/Region/Framework/Scenes/UuidGatherer.cs | 57 +++++++++++----------- 3 files changed, 80 insertions(+), 72 deletions(-) (limited to 'OpenSim/Region/Framework/Scenes') diff --git a/OpenSim/Region/Framework/Scenes/Animation/BinBVHAnimation.cs b/OpenSim/Region/Framework/Scenes/Animation/BinBVHAnimation.cs index 3afc87f..b3b38b2 100644 --- a/OpenSim/Region/Framework/Scenes/Animation/BinBVHAnimation.cs +++ b/OpenSim/Region/Framework/Scenes/Animation/BinBVHAnimation.cs @@ -1,4 +1,4 @@ -/* +/* * Copyright (c) Contributors, http://opensimulator.org/ * See CONTRIBUTORS.TXT for a full list of copyright holders. * @@ -113,31 +113,34 @@ namespace OpenSim.Region.Framework.Scenes.Animation public byte[] ToBytes() { - byte[] outputbytes = new byte[0]; + byte[] outputbytes; - BinaryWriter iostream = new BinaryWriter(new MemoryStream()); - iostream.Write(BinBVHUtil.ES(Utils.UInt16ToBytes(unknown0))); - iostream.Write(BinBVHUtil.ES(Utils.UInt16ToBytes(unknown1))); - iostream.Write(BinBVHUtil.ES(Utils.IntToBytes(Priority))); - iostream.Write(BinBVHUtil.ES(Utils.FloatToBytes(Length))); - iostream.Write(BinBVHUtil.WriteNullTerminatedString(ExpressionName)); - iostream.Write(BinBVHUtil.ES(Utils.FloatToBytes(InPoint))); - iostream.Write(BinBVHUtil.ES(Utils.FloatToBytes(OutPoint))); - iostream.Write(BinBVHUtil.ES(Utils.IntToBytes(Loop ? 1 : 0))); - iostream.Write(BinBVHUtil.ES(Utils.FloatToBytes(EaseInTime))); - iostream.Write(BinBVHUtil.ES(Utils.FloatToBytes(EaseOutTime))); - iostream.Write(BinBVHUtil.ES(Utils.UIntToBytes(HandPose))); - iostream.Write(BinBVHUtil.ES(Utils.UIntToBytes((uint)(Joints.Length)))); - - for (int i = 0; i < Joints.Length; i++) + using (MemoryStream ms = new MemoryStream()) + using (BinaryWriter iostream = new BinaryWriter(ms)) { - Joints[i].WriteBytesToStream(iostream, InPoint, OutPoint); + iostream.Write(BinBVHUtil.ES(Utils.UInt16ToBytes(unknown0))); + iostream.Write(BinBVHUtil.ES(Utils.UInt16ToBytes(unknown1))); + iostream.Write(BinBVHUtil.ES(Utils.IntToBytes(Priority))); + iostream.Write(BinBVHUtil.ES(Utils.FloatToBytes(Length))); + iostream.Write(BinBVHUtil.WriteNullTerminatedString(ExpressionName)); + iostream.Write(BinBVHUtil.ES(Utils.FloatToBytes(InPoint))); + iostream.Write(BinBVHUtil.ES(Utils.FloatToBytes(OutPoint))); + iostream.Write(BinBVHUtil.ES(Utils.IntToBytes(Loop ? 1 : 0))); + iostream.Write(BinBVHUtil.ES(Utils.FloatToBytes(EaseInTime))); + iostream.Write(BinBVHUtil.ES(Utils.FloatToBytes(EaseOutTime))); + iostream.Write(BinBVHUtil.ES(Utils.UIntToBytes(HandPose))); + iostream.Write(BinBVHUtil.ES(Utils.UIntToBytes((uint)(Joints.Length)))); + + for (int i = 0; i < Joints.Length; i++) + { + Joints[i].WriteBytesToStream(iostream, InPoint, OutPoint); + } + iostream.Write(BinBVHUtil.ES(Utils.IntToBytes(0))); + + using (MemoryStream ms2 = (MemoryStream)iostream.BaseStream) + outputbytes = ms2.ToArray(); } - iostream.Write(BinBVHUtil.ES(Utils.IntToBytes(0))); - MemoryStream ms = (MemoryStream)iostream.BaseStream; - outputbytes = ms.ToArray(); - ms.Close(); - iostream.Close(); + return outputbytes; } diff --git a/OpenSim/Region/Framework/Scenes/KeyframeMotion.cs b/OpenSim/Region/Framework/Scenes/KeyframeMotion.cs index 2d489ea..b4a171f 100644 --- a/OpenSim/Region/Framework/Scenes/KeyframeMotion.cs +++ b/OpenSim/Region/Framework/Scenes/KeyframeMotion.cs @@ -280,7 +280,7 @@ namespace OpenSim.Region.Framework.Scenes if (!m_selected) { StopTimer(); - m_serializedPosition = m_group.AbsolutePosition; + m_serializedPosition = m_group.AbsolutePosition; } } } @@ -308,10 +308,11 @@ namespace OpenSim.Region.Framework.Scenes try { - MemoryStream ms = new MemoryStream(data); - BinaryFormatter fmt = new BinaryFormatter(); - - newMotion = (KeyframeMotion)fmt.Deserialize(ms); + using (MemoryStream ms = new MemoryStream(data)) + { + BinaryFormatter fmt = new BinaryFormatter(); + newMotion = (KeyframeMotion)fmt.Deserialize(ms); + } newMotion.m_group = grp; @@ -345,26 +346,26 @@ namespace OpenSim.Region.Framework.Scenes return; m_group = grp; - m_scene = grp.Scene; + m_scene = grp.Scene; Vector3 grppos = grp.AbsolutePosition; - Vector3 offset = grppos - m_serializedPosition; + Vector3 offset = grppos - m_serializedPosition; // avoid doing it more than once // current this will happen dragging a prim to other region m_serializedPosition = grppos; m_basePosition += offset; m_nextPosition += offset; - + m_currentFrame.StartPosition += offset; - m_currentFrame.Position += offset; + m_currentFrame.Position += offset; for (int i = 0; i < m_frames.Count; i++) { Keyframe k = m_frames[i]; k.StartPosition += offset; k.Position += offset; - m_frames[i]=k; + m_frames[i]=k; } if (m_running) @@ -527,7 +528,7 @@ namespace OpenSim.Region.Framework.Scenes { k.Position = pos; // k.Velocity = Vector3.Zero; - } + } k.StartRotation = rot; if (k.Rotation.HasValue) @@ -764,19 +765,22 @@ namespace OpenSim.Region.Framework.Scenes public Byte[] Serialize() { StopTimer(); - MemoryStream ms = new MemoryStream(); - BinaryFormatter fmt = new BinaryFormatter(); SceneObjectGroup tmp = m_group; - m_group = null; - if (!m_selected && tmp != null) - m_serializedPosition = tmp.AbsolutePosition; - fmt.Serialize(ms, this); - m_group = tmp; - if (m_running && !m_waitingCrossing) - StartTimer(); + m_group = null; + if (!m_selected && tmp != null) + m_serializedPosition = tmp.AbsolutePosition; + + using (MemoryStream ms = new MemoryStream()) + { + BinaryFormatter fmt = new BinaryFormatter(); + fmt.Serialize(ms, this); + m_group = tmp; + if (m_running && !m_waitingCrossing) + StartTimer(); - return ms.ToArray(); + return ms.ToArray(); + } } public void StartCrossingCheck() diff --git a/OpenSim/Region/Framework/Scenes/UuidGatherer.cs b/OpenSim/Region/Framework/Scenes/UuidGatherer.cs index a7015e1..d07cc6a 100644 --- a/OpenSim/Region/Framework/Scenes/UuidGatherer.cs +++ b/OpenSim/Region/Framework/Scenes/UuidGatherer.cs @@ -458,35 +458,36 @@ namespace OpenSim.Region.Framework.Scenes if (null == assetBase) return; - MemoryStream ms = new MemoryStream(assetBase.Data); - StreamReader sr = new StreamReader(ms); - - sr.ReadLine(); // Unknown (Version?) - sr.ReadLine(); // Unknown - sr.ReadLine(); // Unknown - sr.ReadLine(); // Name - sr.ReadLine(); // Comment ? - int count = Convert.ToInt32(sr.ReadLine()); // Item count - - for (int i = 0 ; i < count ; i++) + using (MemoryStream ms = new MemoryStream(assetBase.Data)) + using (StreamReader sr = new StreamReader(ms)) { - string type = sr.ReadLine(); - if (type == null) - break; - string name = sr.ReadLine(); - if (name == null) - break; - string id = sr.ReadLine(); - if (id == null) - break; - string unknown = sr.ReadLine(); - if (unknown == null) - break; - - // If it can be parsed as a UUID, it is an asset ID - UUID uuid; - if (UUID.TryParse(id, out uuid)) - assetUuids[uuid] = (sbyte)AssetType.Animation; + sr.ReadLine(); // Unknown (Version?) + sr.ReadLine(); // Unknown + sr.ReadLine(); // Unknown + sr.ReadLine(); // Name + sr.ReadLine(); // Comment ? + int count = Convert.ToInt32(sr.ReadLine()); // Item count + + for (int i = 0 ; i < count ; i++) + { + string type = sr.ReadLine(); + if (type == null) + break; + string name = sr.ReadLine(); + if (name == null) + break; + string id = sr.ReadLine(); + if (id == null) + break; + string unknown = sr.ReadLine(); + if (unknown == null) + break; + + // If it can be parsed as a UUID, it is an asset ID + UUID uuid; + if (UUID.TryParse(id, out uuid)) + assetUuids[uuid] = (sbyte)AssetType.Animation; // the asset is either an Animation or a Sound, but this distinction isn't important + } } } -- cgit v1.1