[Inputmethod] Fix the crash issue due to double free (#2756)
authorInhong <tukkong123@naver.com>
Thu, 18 Mar 2021 05:35:54 +0000 (14:35 +0900)
committerGitHub <noreply@github.com>
Thu, 18 Mar 2021 05:35:54 +0000 (14:35 +0900)
Co-authored-by: InHong Han <inhong1.han@samsung.com>
src/Tizen.Uix.InputMethod/Tizen.Uix.InputMethod/InputMethodEditor.cs [changed mode: 0755->0644]

old mode 100755 (executable)
new mode 100644 (file)
index ce1f2e3..af9e111
@@ -1758,23 +1758,20 @@ namespace Tizen.Uix.InputMethod
         public static void UpdatePreEditString(string str, IEnumerable<PreEditAttribute> attrs)
         {
             IntPtr einaList = IntPtr.Zero;
-            List<GCHandle> attributeHandleList = new List<GCHandle>();
             foreach (PreEditAttribute attribute in attrs)
             {
+                IntPtr attr = IntPtr.Zero;
                 ImePreEditAttributeStruct imePreEditAttribute = new ImePreEditAttributeStruct();
                 imePreEditAttribute.start = attribute.Start;
                 imePreEditAttribute.length = attribute.Length;
                 imePreEditAttribute.type = (int)attribute.Type;
                 imePreEditAttribute.value = attribute.Value;
-                GCHandle attributeHandle = GCHandle.Alloc(imePreEditAttribute, GCHandleType.Pinned);
-                attributeHandleList.Add(attributeHandle);
-                einaList = Interop.EinaList.EinaListAppend(einaList, attributeHandle.AddrOfPinnedObject());
+                attr = Marshal.AllocHGlobal(Marshal.SizeOf(imePreEditAttribute));
+                Marshal.WriteIntPtr(attr, IntPtr.Zero);
+                Marshal.StructureToPtr(imePreEditAttribute, attr, false);
+                einaList = Interop.EinaList.EinaListAppend(einaList, attr);
             }
             ErrorCode error = ImeUpdatePreeditString(str, einaList);
-            foreach (GCHandle handle in attributeHandleList)
-            {
-                handle.Free();
-            }
             if (error != ErrorCode.None)
             {
                 Log.Error(LogTag, "UpdatePreEditString Failed with error " + error);