[ImageUtil] Fix crash issue (#919)
authorhsgwon <haesu.gwon@samsung.com>
Fri, 5 Jul 2019 02:42:39 +0000 (11:42 +0900)
committerGitHub <noreply@github.com>
Fri, 5 Jul 2019 02:42:39 +0000 (11:42 +0900)
* [MediaController] Fix crash issue caused by GC memory collecting.

src/Tizen.Multimedia.Util/ImageUtil/ImageDecoder.cs
src/Tizen.Multimedia.Util/Interop/Interop.ImageUtil.Decode.cs

index 4e47ed7..81824ec 100644 (file)
@@ -126,7 +126,6 @@ namespace Tizen.Multimedia.Util
             var pathPtr = Marshal.StringToHGlobalAnsi(inputFilePath);
             try
             {
-
                 SetInputPath(Handle, pathPtr).ThrowIfFailed("Failed to set input file path for decoding");
                 return await DecodeAsync();
             }
@@ -204,20 +203,26 @@ namespace Tizen.Multimedia.Util
 
         private IEnumerable<BitmapFrame> RunDecoding()
         {
-            IntPtr outBuffer = IntPtr.Zero;
+            IntPtr outBuffer = Marshal.AllocHGlobal(Marshal.SizeOf(typeof(IntPtr)));
+            Marshal.WriteIntPtr(outBuffer, IntPtr.Zero);
 
             try
             {
-                SetOutputBuffer(Handle, out outBuffer).ThrowIfFailed("Failed to decode given image");
+                SetOutputBuffer(Handle, outBuffer).ThrowIfFailed("Failed to decode given image");
 
                 DecodeRun(Handle, out var width, out var height, out var size).
                     ThrowIfFailed("Failed to decode");
 
-                yield return new BitmapFrame(outBuffer, width, height, (int)size);
+                yield return new BitmapFrame(Marshal.ReadIntPtr(outBuffer), width, height, (int)size);
             }
             finally
             {
-                LibcSupport.Free(outBuffer);
+                if (Marshal.ReadIntPtr(outBuffer) != IntPtr.Zero)
+                {
+                    LibcSupport.Free(Marshal.ReadIntPtr(outBuffer));
+                }
+
+                Marshal.FreeHGlobal(outBuffer);
             }
         }
 
index f7636c6..938db12 100644 (file)
@@ -39,7 +39,7 @@ internal static partial class Interop
         internal static extern ImageUtilError SetInputBuffer(ImageDecoderHandle handle, byte[] srcBuffer, ulong srcSize);
 
         [DllImport(Libraries.ImageUtil, EntryPoint = "image_util_decode_set_output_buffer")]
-        internal static extern ImageUtilError SetOutputBuffer(ImageDecoderHandle handle, out IntPtr dstBuffer);
+        internal static extern ImageUtilError SetOutputBuffer(ImageDecoderHandle handle, IntPtr dstBuffer);
 
         [DllImport(Libraries.ImageUtil, EntryPoint = "image_util_decode_set_colorspace")]
         internal static extern ImageUtilError SetColorspace(ImageDecoderHandle handle, ImageColorSpace colorspace);