From 99ac770abbe3a95887c4b10c82f3985aa878eeef 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 --- .../Agent/TextureSender/J2KDecoderModule.cs | 6 ++- .../Avatar/Friends/FriendsRequestHandler.cs | 15 +----- .../World/Archiver/ArchiveReadRequest.cs | 19 ++++---- .../World/Archiver/ArchiveWriteRequest.cs | 9 ++-- .../World/Estate/EstateManagementModule.cs | 5 +- .../World/Estate/XEstateRequestHandler.cs | 12 +---- .../World/Serialiser/SerialiseObjects.cs | 30 +++++++----- .../CoreModules/World/WorldMap/WorldMapModule.cs | 7 +-- OpenSim/Region/DataSnapshot/DataSnapshotManager.cs | 35 +++++++------ .../Framework/Scenes/Animation/BinBVHAnimation.cs | 49 ++++++++++--------- OpenSim/Region/Framework/Scenes/KeyframeMotion.cs | 46 +++++++++-------- OpenSim/Region/Framework/Scenes/UuidGatherer.cs | 57 +++++++++++----------- 12 files changed, 143 insertions(+), 147 deletions(-) (limited to 'OpenSim/Region') diff --git a/OpenSim/Region/CoreModules/Agent/TextureSender/J2KDecoderModule.cs b/OpenSim/Region/CoreModules/Agent/TextureSender/J2KDecoderModule.cs index 732781a..5cdcab9 100644 --- a/OpenSim/Region/CoreModules/Agent/TextureSender/J2KDecoderModule.cs +++ b/OpenSim/Region/CoreModules/Agent/TextureSender/J2KDecoderModule.cs @@ -231,7 +231,11 @@ namespace OpenSim.Region.CoreModules.Agent.TextureSender { try { - List layerStarts = CSJ2K.J2kImage.GetLayerBoundaries(new MemoryStream(j2kData)); + List layerStarts; + using (MemoryStream ms = new MemoryStream(j2kData)) + { + layerStarts = CSJ2K.J2kImage.GetLayerBoundaries(ms); + } if (layerStarts != null && layerStarts.Count > 0) { diff --git a/OpenSim/Region/CoreModules/Avatar/Friends/FriendsRequestHandler.cs b/OpenSim/Region/CoreModules/Avatar/Friends/FriendsRequestHandler.cs index ff87ece..13512a2 100644 --- a/OpenSim/Region/CoreModules/Avatar/Friends/FriendsRequestHandler.cs +++ b/OpenSim/Region/CoreModules/Avatar/Friends/FriendsRequestHandler.cs @@ -1,4 +1,4 @@ -/* +/* * Copyright (c) Contributors, http://opensimulator.org/ * See CONTRIBUTORS.TXT for a full list of copyright holders. * @@ -289,18 +289,7 @@ namespace OpenSim.Region.CoreModules.Avatar.Friends rootElement.AppendChild(result); - return DocToBytes(doc); - } - - private byte[] DocToBytes(XmlDocument doc) - { - MemoryStream ms = new MemoryStream(); - XmlTextWriter xw = new XmlTextWriter(ms, null); - xw.Formatting = Formatting.Indented; - doc.WriteTo(xw); - xw.Flush(); - - return ms.ToArray(); + return Util.DocToBytes(doc); } #endregion diff --git a/OpenSim/Region/CoreModules/World/Archiver/ArchiveReadRequest.cs b/OpenSim/Region/CoreModules/World/Archiver/ArchiveReadRequest.cs index e9c6fb6..c8056d4 100644 --- a/OpenSim/Region/CoreModules/World/Archiver/ArchiveReadRequest.cs +++ b/OpenSim/Region/CoreModules/World/Archiver/ArchiveReadRequest.cs @@ -918,17 +918,18 @@ namespace OpenSim.Region.CoreModules.World.Archiver { ITerrainModule terrainModule = scene.RequestModuleInterface(); - MemoryStream ms = new MemoryStream(data); - if (m_displacement != Vector3.Zero || m_rotation != 0f) + using (MemoryStream ms = new MemoryStream(data)) { - Vector2 rotationCenter = new Vector2(m_rotationCenter.X, m_rotationCenter.Y); - terrainModule.LoadFromStream(terrainPath, m_displacement, m_rotation, rotationCenter, ms); - } - else - { - terrainModule.LoadFromStream(terrainPath, ms); + if (m_displacement != Vector3.Zero || m_rotation != 0f) + { + Vector2 rotationCenter = new Vector2(m_rotationCenter.X, m_rotationCenter.Y); + terrainModule.LoadFromStream(terrainPath, m_displacement, m_rotation, rotationCenter, ms); + } + else + { + terrainModule.LoadFromStream(terrainPath, ms); + } } - ms.Close(); m_log.DebugFormat("[ARCHIVER]: Restored terrain {0}", terrainPath); diff --git a/OpenSim/Region/CoreModules/World/Archiver/ArchiveWriteRequest.cs b/OpenSim/Region/CoreModules/World/Archiver/ArchiveWriteRequest.cs index 136a16e..448147a 100644 --- a/OpenSim/Region/CoreModules/World/Archiver/ArchiveWriteRequest.cs +++ b/OpenSim/Region/CoreModules/World/Archiver/ArchiveWriteRequest.cs @@ -569,10 +569,11 @@ namespace OpenSim.Region.CoreModules.World.Archiver string terrainPath = String.Format("{0}{1}{2}.r32", regionDir, ArchiveConstants.TERRAINS_PATH, scene.RegionInfo.RegionName); - MemoryStream ms = new MemoryStream(); - scene.RequestModuleInterface().SaveToStream(terrainPath, ms); - m_archiveWriter.WriteFile(terrainPath, ms.ToArray()); - ms.Close(); + using (MemoryStream ms = new MemoryStream()) + { + scene.RequestModuleInterface().SaveToStream(terrainPath, ms); + m_archiveWriter.WriteFile(terrainPath, ms.ToArray()); + } m_log.InfoFormat("[ARCHIVER]: Adding scene objects to archive."); diff --git a/OpenSim/Region/CoreModules/World/Estate/EstateManagementModule.cs b/OpenSim/Region/CoreModules/World/Estate/EstateManagementModule.cs index 9fac9ff..80fa08a 100644 --- a/OpenSim/Region/CoreModules/World/Estate/EstateManagementModule.cs +++ b/OpenSim/Region/CoreModules/World/Estate/EstateManagementModule.cs @@ -1121,9 +1121,8 @@ namespace OpenSim.Region.CoreModules.World.Estate { try { - MemoryStream terrainStream = new MemoryStream(terrainData); - terr.LoadFromStream(filename, terrainStream); - terrainStream.Close(); + using (MemoryStream terrainStream = new MemoryStream(terrainData)) + terr.LoadFromStream(filename, terrainStream); FileInfo x = new FileInfo(filename); remoteClient.SendAlertMessage("Your terrain was loaded as a " + x.Extension + " file. It may take a few moments to appear."); diff --git a/OpenSim/Region/CoreModules/World/Estate/XEstateRequestHandler.cs b/OpenSim/Region/CoreModules/World/Estate/XEstateRequestHandler.cs index 2366767..ec5af2b 100644 --- a/OpenSim/Region/CoreModules/World/Estate/XEstateRequestHandler.cs +++ b/OpenSim/Region/CoreModules/World/Estate/XEstateRequestHandler.cs @@ -281,18 +281,8 @@ namespace OpenSim.Region.CoreModules.World.Estate rootElement.AppendChild(result); - return DocToBytes(doc); + return Util.DocToBytes(doc); } - private byte[] DocToBytes(XmlDocument doc) - { - MemoryStream ms = new MemoryStream(); - XmlTextWriter xw = new XmlTextWriter(ms, null); - xw.Formatting = Formatting.Indented; - doc.WriteTo(xw); - xw.Flush(); - - return ms.ToArray(); - } } } diff --git a/OpenSim/Region/CoreModules/World/Serialiser/SerialiseObjects.cs b/OpenSim/Region/CoreModules/World/Serialiser/SerialiseObjects.cs index 328fbf0..65f464a 100644 --- a/OpenSim/Region/CoreModules/World/Serialiser/SerialiseObjects.cs +++ b/OpenSim/Region/CoreModules/World/Serialiser/SerialiseObjects.cs @@ -54,13 +54,14 @@ namespace OpenSim.Region.CoreModules.World.Serialiser { string xmlstream = GetObjectXml(scene); - MemoryStream stream = ReformatXmlString(xmlstream); - - stream.Seek(0, SeekOrigin.Begin); - CreateXmlFile(stream, fileName); + using (MemoryStream stream = ReformatXmlString(xmlstream)) + { + stream.Seek(0, SeekOrigin.Begin); + CreateXmlFile(stream, fileName); - stream.Seek(0, SeekOrigin.Begin); - CreateCompressedXmlFile(stream, fileName); + stream.Seek(0, SeekOrigin.Begin); + CreateCompressedXmlFile(stream, fileName); + } } private static MemoryStream ReformatXmlString(string xmlstream) @@ -112,13 +113,16 @@ namespace OpenSim.Region.CoreModules.World.Serialiser { #region GZip Compressed Version - FileStream objectsFileCompressed = new FileStream(fileName + ".gzs", FileMode.Create); - MemoryStream gzipMSStream = new MemoryStream(); - GZipStream gzipStream = new GZipStream(gzipMSStream, CompressionMode.Compress); - xmlStream.WriteTo(gzipStream); - gzipMSStream.WriteTo(objectsFileCompressed); - objectsFileCompressed.Flush(); - objectsFileCompressed.Close(); + using (FileStream objectsFileCompressed = new FileStream(fileName + ".gzs", FileMode.Create)) + using (MemoryStream gzipMSStream = new MemoryStream()) + { + using (GZipStream gzipStream = new GZipStream(gzipMSStream, CompressionMode.Compress, true)) + { + xmlStream.WriteTo(gzipStream); + } + + gzipMSStream.WriteTo(objectsFileCompressed); + } #endregion } diff --git a/OpenSim/Region/CoreModules/World/WorldMap/WorldMapModule.cs b/OpenSim/Region/CoreModules/World/WorldMap/WorldMapModule.cs index 2d28d6e..995e297 100644 --- a/OpenSim/Region/CoreModules/World/WorldMap/WorldMapModule.cs +++ b/OpenSim/Region/CoreModules/World/WorldMap/WorldMapModule.cs @@ -1145,7 +1145,7 @@ namespace OpenSim.Region.CoreModules.World.WorldMap if (myMapImageJPEG.Length == 0) { - MemoryStream imgstream = new MemoryStream(); + MemoryStream imgstream = null; Bitmap mapTexture = new Bitmap(1,1); ManagedImage managedImage; Image image = (Image)mapTexture; @@ -1192,10 +1192,7 @@ namespace OpenSim.Region.CoreModules.World.WorldMap image.Dispose(); if (imgstream != null) - { - imgstream.Close(); imgstream.Dispose(); - } } } else @@ -1639,4 +1636,4 @@ namespace OpenSim.Region.CoreModules.World.WorldMap public uint itemtype; public ulong regionhandle; } -} \ No newline at end of file +} diff --git a/OpenSim/Region/DataSnapshot/DataSnapshotManager.cs b/OpenSim/Region/DataSnapshot/DataSnapshotManager.cs index 4a06f6e..33899aa 100644 --- a/OpenSim/Region/DataSnapshot/DataSnapshotManager.cs +++ b/OpenSim/Region/DataSnapshot/DataSnapshotManager.cs @@ -373,24 +373,27 @@ namespace OpenSim.Region.DataSnapshot for (int i = 0; i < services.Length; i++) { string url = services[i].Trim(); - RestClient cli = new RestClient(url); - cli.AddQueryParameter("service", serviceName); - cli.AddQueryParameter("host", m_hostname); - cli.AddQueryParameter("port", m_listener_port); - cli.AddQueryParameter("secret", m_Secret.ToString()); - cli.RequestMethod = "GET"; - try - { - reply = cli.Request(null); - } - catch (WebException) + using (RestClient cli = new RestClient(url)) { - m_log.Warn("[DATASNAPSHOT]: Unable to notify " + url); - } - catch (Exception e) - { - m_log.Warn("[DATASNAPSHOT]: Ignoring unknown exception " + e.ToString()); + cli.AddQueryParameter("service", serviceName); + cli.AddQueryParameter("host", m_hostname); + cli.AddQueryParameter("port", m_listener_port); + cli.AddQueryParameter("secret", m_Secret.ToString()); + cli.RequestMethod = "GET"; + try + { + reply = cli.Request(null); + } + catch (WebException) + { + m_log.Warn("[DATASNAPSHOT]: Unable to notify " + url); + } + catch (Exception e) + { + m_log.Warn("[DATASNAPSHOT]: Ignoring unknown exception " + e.ToString()); + } } + byte[] response = new byte[1024]; // int n = 0; try 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