From c82db4df0cf36f307b4345c9ef4dca516502e870 Mon Sep 17 00:00:00 2001 From: hjhun <36876573+hjhun@users.noreply.github.com> Date: Mon, 31 Oct 2022 11:13:48 +0900 Subject: [PATCH] [Applications.Common] Increment the reference counter on SafeHandle instances (#4693) * Increment the reference counter on SafeHandle instances. To prevent the common language runtime from reclaiming memory used by a handle, this patch adds calling DangerousAddRef() before calling DangerousGetHandle(). Signed-off-by: Hwankyu Jhun * Modify implementation of using SafeHandle Signed-off-by: Hwankyu Jhun * Remove unnecesary blank line Signed-off-by: Hwankyu Jhun Signed-off-by: Hwankyu Jhun --- .../Tizen.Applications.RPCPort/Parcel.cs | 8 +++++- .../Tizen.Applications/AppControl.cs | 29 +++++++++++++++++++--- .../Tizen.Applications/Bundle.cs | 23 ++++++++++++++--- 3 files changed, 52 insertions(+), 8 deletions(-) diff --git a/src/Tizen.Applications.Common/Tizen.Applications.RPCPort/Parcel.cs b/src/Tizen.Applications.Common/Tizen.Applications.RPCPort/Parcel.cs index 99f6c34..c1dd827 100755 --- a/src/Tizen.Applications.Common/Tizen.Applications.RPCPort/Parcel.cs +++ b/src/Tizen.Applications.Common/Tizen.Applications.RPCPort/Parcel.cs @@ -425,7 +425,13 @@ namespace Tizen.Applications.RPCPort { Interop.LibRPCPort.Parcel.ReadBundle(_handle, out IntPtr b); - return new Bundle(new SafeBundleHandle(b, true)); + Bundle bundle = null; + using (SafeBundleHandle safeBundleHandle = new SafeBundleHandle(b, true)) + { + bundle = new Bundle(safeBundleHandle); + } + + return bundle; } /// diff --git a/src/Tizen.Applications.Common/Tizen.Applications/AppControl.cs b/src/Tizen.Applications.Common/Tizen.Applications/AppControl.cs index 20ead07..4f911a0 100755 --- a/src/Tizen.Applications.Common/Tizen.Applications/AppControl.cs +++ b/src/Tizen.Applications.Common/Tizen.Applications/AppControl.cs @@ -108,13 +108,34 @@ namespace Tizen.Applications { if (handle == null) { - throw new ArgumentNullException("handle"); + throw new ArgumentNullException(nameof(handle)); } - Interop.AppControl.ErrorCode err = Interop.AppControl.DangerousClone(out _handle, handle.DangerousGetHandle()); - if (err != Interop.AppControl.ErrorCode.None) + if (handle.IsInvalid) { - throw new InvalidOperationException("Failed to clone the appcontrol handle. Err = " + err); + throw new ArgumentNullException(nameof(handle), "handle is invalid"); + } + + bool mustRelease = false; + try + { + handle.DangerousAddRef(ref mustRelease); + Interop.AppControl.ErrorCode err = Interop.AppControl.DangerousClone(out _handle, handle.DangerousGetHandle()); + if (err != Interop.AppControl.ErrorCode.None) + { + throw new InvalidOperationException("Failed to clone the appcontrol handle. Err = " + err); + } + } + catch (ObjectDisposedException e) + { + throw new ArgumentNullException(nameof(handle), e.Message); + } + finally + { + if (mustRelease) + { + handle.DangerousRelease(); + } } } diff --git a/src/Tizen.Applications.Common/Tizen.Applications/Bundle.cs b/src/Tizen.Applications.Common/Tizen.Applications/Bundle.cs index 791cfad..3cd86a9 100644 --- a/src/Tizen.Applications.Common/Tizen.Applications/Bundle.cs +++ b/src/Tizen.Applications.Common/Tizen.Applications/Bundle.cs @@ -65,15 +65,32 @@ namespace Tizen.Applications { if (handle == null) { - throw new ArgumentNullException("handle"); + throw new ArgumentNullException(nameof(handle)); } if (handle.IsInvalid) { - throw new ArgumentNullException("handle", "handle is invalid"); + throw new ArgumentNullException(nameof(handle), "handle is invalid"); + } + + bool mustRelease = false; + try + { + handle.DangerousAddRef(ref mustRelease); + _handle = Interop.Bundle.DangerousClone(handle.DangerousGetHandle()); + } + catch (ObjectDisposedException e) + { + throw new ArgumentNullException(nameof(handle), e.Message); + } + finally + { + if (mustRelease) + { + handle.DangerousRelease(); + } } - _handle = Interop.Bundle.DangerousClone(handle.DangerousGetHandle()); _keys = new HashSet(); Interop.Bundle.Iterator iterator = (string key, int type, IntPtr keyval, IntPtr userData) => { -- 2.7.4