From 6744ec95a9a5653fff2f8ba21bfbfc8bb82c9ef5 Mon Sep 17 00:00:00 2001
From: UbitUmarov
Date: Wed, 24 Aug 2016 01:05:01 +0100
Subject:  DynamicTextureModule memory leaks

---
 .../DynamicTexture/DynamicTextureModule.cs         | 63 ++++++++++++----------
 1 file changed, 35 insertions(+), 28 deletions(-)

diff --git a/OpenSim/Region/CoreModules/Scripting/DynamicTexture/DynamicTextureModule.cs b/OpenSim/Region/CoreModules/Scripting/DynamicTexture/DynamicTextureModule.cs
index a686a4d..665d7f7 100644
--- a/OpenSim/Region/CoreModules/Scripting/DynamicTexture/DynamicTextureModule.cs
+++ b/OpenSim/Region/CoreModules/Scripting/DynamicTexture/DynamicTextureModule.cs
@@ -553,37 +553,44 @@ namespace OpenSim.Region.CoreModules.Scripting.DynamicTexture
                 ManagedImage managedImage;
                 Image image;
 
-                if (OpenJPEG.DecodeToImage(frontImage, out managedImage, out image))
+                if (!OpenJPEG.DecodeToImage(frontImage, out managedImage, out image) || image == null)
+                    return null;
+
+                Bitmap image1 = new Bitmap(image);
+                image.Dispose();
+                
+                if (!OpenJPEG.DecodeToImage(backImage, out managedImage, out image) || image == null)
                 {
-                    Bitmap image1 = new Bitmap(image);
+                    image1.Dispose();
+                    return null;
+                }
 
-                    if (OpenJPEG.DecodeToImage(backImage, out managedImage, out image))
-                    {
-                        Bitmap image2 = new Bitmap(image);
+                Bitmap image2 = new Bitmap(image);
+                image.Dispose();
 
-                        if (setNewAlpha)
-                            SetAlpha(ref image1, newAlpha);
+                if (setNewAlpha)
+                    SetAlpha(ref image1, newAlpha);
 
-                        Bitmap joint = MergeBitMaps(image1, image2);
+                using(Bitmap joint = MergeBitMaps(image1, image2))
+                {
+                    image1.Dispose();
+                    image2.Dispose();
 
-                        byte[] result = new byte[0];
+                    byte[] result = new byte[0];
 
-                        try
-                        {
-                            result = OpenJPEG.EncodeFromImage(joint, true);
-                        }
-                        catch (Exception e)
-                        {
-                            m_log.ErrorFormat(
-                                "[DYNAMICTEXTUREMODULE]: OpenJpeg Encode Failed.  Exception {0}{1}",
+                    try
+                    {
+                        result = OpenJPEG.EncodeFromImage(joint, true);
+                    }
+                    catch (Exception e)
+                    {
+                        m_log.ErrorFormat(
+                        "[DYNAMICTEXTUREMODULE]: OpenJpeg Encode Failed.  Exception {0}{1}",
                                 e.Message, e.StackTrace);
-                        }
-
-                        return result;
                     }
-                }
 
-                return null;
+                    return result;
+                }
             }
 
             public Bitmap MergeBitMaps(Bitmap front, Bitmap back)
@@ -592,12 +599,12 @@ namespace OpenSim.Region.CoreModules.Scripting.DynamicTexture
                 Graphics jG;
 
                 joint = new Bitmap(back.Width, back.Height, PixelFormat.Format32bppArgb);
-                jG = Graphics.FromImage(joint);
-
-                jG.DrawImage(back, 0, 0, back.Width, back.Height);
-                jG.DrawImage(front, 0, 0, back.Width, back.Height);
-
-                return joint;
+                using(jG = Graphics.FromImage(joint))
+                {
+                    jG.DrawImage(back, 0, 0, back.Width, back.Height);
+                    jG.DrawImage(front, 0, 0, back.Width, back.Height);
+                    return joint;
+                }
             }
 
             private void SetAlpha(ref Bitmap b, byte alpha)
-- 
cgit v1.1