From 52a80f17423afc0049117c812cacab159f544639 Mon Sep 17 00:00:00 2001 From: UbitUmarov Date: Mon, 22 Aug 2016 05:47:19 +0100 Subject: workaround potencial memory leak --- .../Handlers/GetTexture/GetTextureHandler.cs | 35 ++++++++++++---------- 1 file changed, 19 insertions(+), 16 deletions(-) (limited to 'OpenSim/Capabilities/Handlers/GetTexture/GetTextureHandler.cs') diff --git a/OpenSim/Capabilities/Handlers/GetTexture/GetTextureHandler.cs b/OpenSim/Capabilities/Handlers/GetTexture/GetTextureHandler.cs index ac99692..8215124 100644 --- a/OpenSim/Capabilities/Handlers/GetTexture/GetTextureHandler.cs +++ b/OpenSim/Capabilities/Handlers/GetTexture/GetTextureHandler.cs @@ -354,9 +354,9 @@ namespace OpenSim.Capabilities.Handlers byte[] data = new byte[0]; MemoryStream imgstream = new MemoryStream(); - Bitmap mTexture = new Bitmap(1, 1); - ManagedImage managedImage; - Image image = (Image)mTexture; + Bitmap mTexture = null; + ManagedImage managedImage = null; + Image image = null; try { @@ -365,25 +365,26 @@ namespace OpenSim.Capabilities.Handlers imgstream = new MemoryStream(); // Decode image to System.Drawing.Image - if (OpenJPEG.DecodeToImage(texture.Data, out managedImage, out image)) + if (OpenJPEG.DecodeToImage(texture.Data, out managedImage, out image) && image != null) { // Save to bitmap mTexture = new Bitmap(image); - EncoderParameters myEncoderParameters = new EncoderParameters(); - myEncoderParameters.Param[0] = new EncoderParameter(Encoder.Quality, 95L); - - // Save bitmap to stream - ImageCodecInfo codec = GetEncoderInfo("image/" + format); - if (codec != null) + using(EncoderParameters myEncoderParameters = new EncoderParameters()) { - mTexture.Save(imgstream, codec, myEncoderParameters); + myEncoderParameters.Param[0] = new EncoderParameter(Encoder.Quality,95L); + + // Save bitmap to stream + ImageCodecInfo codec = GetEncoderInfo("image/" + format); + if (codec != null) + { + mTexture.Save(imgstream, codec, myEncoderParameters); // Write the stream to a byte array for output - data = imgstream.ToArray(); + data = imgstream.ToArray(); + } + else + m_log.WarnFormat("[GETTEXTURE]: No such codec {0}", format); } - else - m_log.WarnFormat("[GETTEXTURE]: No such codec {0}", format); - } } catch (Exception e) @@ -399,7 +400,9 @@ namespace OpenSim.Capabilities.Handlers if (image != null) image.Dispose(); - + + if(managedImage != null) + managedImage.Clear(); if (imgstream != null) { imgstream.Close(); -- cgit v1.1