[Account] Unmanaged resource handling (#685)
authorabhishekvijay <40332812+abhishekvijay@users.noreply.github.com>
Mon, 11 Feb 2019 07:04:48 +0000 (12:34 +0530)
committerWonYoung Choi <wy80.choi@samsung.com>
Mon, 11 Feb 2019 07:04:48 +0000 (16:04 +0900)
* Resolving crash issue for unmanaged resources

Signed-off-by: Abhishek Vijay <abhishek.v@samsung.com>
* Handling unused local variable

Signed-off-by: Abhishek Vijay <abhishek.v@samsung.com>
src/Tizen.Account.AccountManager/Interop/Interop.Account.cs
src/Tizen.Account.AccountManager/Tizen.Account.AccountManager/AccountService.cs [changed mode: 0644->0755]

index 90489bc..256c69d 100644 (file)
@@ -32,6 +32,8 @@ internal static partial class Interop
         [DllImport(Libraries.AccountSvc, EntryPoint = "account_create", CallingConvention = CallingConvention.Cdecl)]
         internal static extern int Create(out SafeAccountHandle handle);
 
+        [DllImport(Libraries.AccountSvc, EntryPoint = "account_create", CallingConvention = CallingConvention.Cdecl)]
+        internal static extern int CreateUnmanagedHandle(out IntPtr handle);
 
         [DllImport(Libraries.AccountSvc, EntryPoint = "account_destroy", CallingConvention = CallingConvention.Cdecl)]
         internal static extern int Destroy(IntPtr handle);
old mode 100644 (file)
new mode 100755 (executable)
index 374adcc..e24bf85
@@ -141,15 +141,22 @@ namespace Tizen.Account.AccountManager
         /// <exception cref="NotSupportedException">The required feature is not supported.</exception>
         public static Account GetAccountById(int accountId)
         {
-            Account account = Account.CreateAccount();
-            SafeAccountHandle handle = account.SafeAccountHandle;
+            AccountError err = (AccountError)Interop.Account.CreateUnmanagedHandle(out IntPtr handle);
+            if (err != AccountError.None)
+            {
+                Log.Warn(AccountErrorFactory.LogTag, "Failed to create handle");
+                throw AccountErrorFactory.CreateException(err, "Failed to create unmanaged handle");
+            }
 
-            AccountError res = (AccountError)Interop.AccountService.QueryAccountById(accountId, ref handle);
+            SafeAccountHandle accountHandle = new SafeAccountHandle(handle, false);
+
+            AccountError res = (AccountError)Interop.AccountService.QueryAccountById(accountId, ref accountHandle);
             if (res != AccountError.None)
             {
                 throw AccountErrorFactory.CreateException(res, "Failed to get accounts from the database for account id: " + accountId);
             }
-            Account ref_account = new Account(handle);
+
+            Account ref_account = new Account(accountHandle);
 
             return ref_account;
         }