From f04aa0bc347897a3c303c0c5849d91cba9a59700 Mon Sep 17 00:00:00 2001 From: hsgwon Date: Mon, 8 Jul 2019 14:33:35 +0900 Subject: [PATCH] [ImageUtil] Fix crash issue (#925) --- .../ImageUtil/ImageDecoder.cs | 46 +++++++++++----------- .../Interop/Interop.ImageUtil.Decode.cs | 2 +- 2 files changed, 25 insertions(+), 23 deletions(-) diff --git a/src/Tizen.Multimedia.Util/ImageUtil/ImageDecoder.cs b/src/Tizen.Multimedia.Util/ImageUtil/ImageDecoder.cs index 43b69ad..5b35959 100644 --- a/src/Tizen.Multimedia.Util/ImageUtil/ImageDecoder.cs +++ b/src/Tizen.Multimedia.Util/ImageUtil/ImageDecoder.cs @@ -20,6 +20,7 @@ using System.Diagnostics; using System.IO; using System.Linq; using System.Runtime.InteropServices; +using System.Threading; using System.Threading.Tasks; using static Interop; using static Interop.Decode; @@ -201,37 +202,38 @@ namespace Tizen.Multimedia.Util } } - internal Task> DecodeAsync() + private IEnumerable RunDecoding() { - Initialize(Handle); + IntPtr outBuffer = Marshal.AllocHGlobal(Marshal.SizeOf(typeof(IntPtr))); + Marshal.WriteIntPtr(outBuffer, IntPtr.Zero); - IntPtr outBuffer = IntPtr.Zero; - SetOutputBuffer(Handle, out outBuffer).ThrowIfFailed("Failed to decode given image"); + try + { + SetOutputBuffer(Handle, outBuffer).ThrowIfFailed("Failed to decode given image"); - var tcs = new TaskCompletionSource>(); + DecodeRun(Handle, out var width, out var height, out var size). + ThrowIfFailed("Failed to decode"); - Task.Run(() => + yield return new BitmapFrame(Marshal.ReadIntPtr(outBuffer), width, height, (int)size); + } + finally { - try + if (Marshal.ReadIntPtr(outBuffer) != IntPtr.Zero) { - int width, height; - ulong size; + LibcSupport.Free(Marshal.ReadIntPtr(outBuffer)); + } - DecodeRun(Handle, out width, out height, out size).ThrowIfFailed("Failed to decode"); + Marshal.FreeHGlobal(outBuffer); + } + } - tcs.SetResult(new[] { new BitmapFrame(outBuffer, width, height, (int)size) }); - } - catch (Exception e) - { - tcs.TrySetException(e); - } - finally - { - LibcSupport.Free(outBuffer); - } - }); + internal Task> DecodeAsync() + { + Initialize(Handle); - return tcs.Task; + return Task.Factory.StartNew(RunDecoding, CancellationToken.None, + TaskCreationOptions.DenyChildAttach | TaskCreationOptions.LongRunning, + TaskScheduler.Default); } internal virtual void Initialize(ImageDecoderHandle handle) diff --git a/src/Tizen.Multimedia.Util/Interop/Interop.ImageUtil.Decode.cs b/src/Tizen.Multimedia.Util/Interop/Interop.ImageUtil.Decode.cs index f7636c6..938db12 100644 --- a/src/Tizen.Multimedia.Util/Interop/Interop.ImageUtil.Decode.cs +++ b/src/Tizen.Multimedia.Util/Interop/Interop.ImageUtil.Decode.cs @@ -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); -- 2.7.4