Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[NUI] Make Window events safe while window is being collected #6503

Merged
merged 3 commits into from
Dec 10, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
61 changes: 59 additions & 2 deletions src/Tizen.NUI/src/public/Common/BaseHandle.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
*
*/
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Runtime.CompilerServices;
using Tizen.NUI.Binding;
Expand All @@ -33,6 +34,8 @@ namespace Tizen.NUI
/// <since_tizen> 3 </since_tizen>
public class BaseHandle : Element, global::System.IDisposable
{
private static Dictionary<IntPtr, HashSet<object>> nativeBindedHolder = new Dictionary<IntPtr, HashSet<object>>();

static internal void Preload()
{
// Do nothing. Just call for load static values.
Expand Down Expand Up @@ -287,7 +290,7 @@ public static explicit operator bool(BaseHandle handle)
}

/// <summary>
/// Logical OR operator for ||.
/// Logical OR operator for ||.
/// It's possible when doing a || this function (opBitwiseOr) is never called due to short circuiting.
/// </summary>
/// <param name="x">The first BaseHandle to be compared.</param>
Expand Down Expand Up @@ -375,7 +378,7 @@ public void Dispose()

/// <summary>
/// Hidden API (Inhouse API).
/// Dispose.
/// Dispose.
/// Releases any unmanaged resources used by this object. Can also dispose any other disposable objects.
/// </summary>
/// <remarks>
Expand Down Expand Up @@ -622,6 +625,9 @@ protected virtual void Dispose(DisposeTypes type)
if (SwigCPtr.Handle != IntPtr.Zero)
{
var nativeSwigCPtr = swigCPtr.Handle;

ClearHolder(nativeSwigCPtr);

swigCPtr = new global::System.Runtime.InteropServices.HandleRef(null, global::System.IntPtr.Zero);
if (swigCMemOwn)
{
Expand Down Expand Up @@ -654,6 +660,49 @@ protected virtual void ReleaseSwigCPtr(System.Runtime.InteropServices.HandleRef
Interop.BaseHandle.DeleteBaseHandle(swigCPtr.Handle);
}

/// <summary>
/// Adds the specified object to the set of objects that have been bound to the native object.
/// </summary>
/// <param name="obj">The object to add.</param>
[EditorBrowsable(EditorBrowsableState.Never)]
protected void AddToNativeHolder(object obj)
{
if (IsDisposedOrQueued)
{
return;
}

if (!nativeBindedHolder.TryGetValue(swigCPtr.Handle, out var holders))
{
nativeBindedHolder.Add(swigCPtr.Handle, holders = new HashSet<object>());
}

holders.Add(obj);
}

/// <summary>
/// Removes the specified object from the set of objects that have been bound to the native object.
/// </summary>
/// <param name="obj">The object to remove.</param>
[EditorBrowsable(EditorBrowsableState.Never)]
protected void RemoveFromNativeHolder(object obj)
{
if (IsDisposedOrQueued)
{
return;
}

if (nativeBindedHolder.TryGetValue(swigCPtr.Handle, out var holders))
{
holders.Remove(obj);

if (holders.Count == 0)
{
nativeBindedHolder.Remove(swigCPtr.Handle);
}
}
}

/// <summary>
/// Contains event arguments for the FocusChangeRequested event.
/// </summary>
Expand Down Expand Up @@ -728,6 +777,14 @@ internal bool IsNativeHandleInvalid()
[EditorBrowsable(EditorBrowsableState.Never)]
protected internal bool IsDisposeQueued => isDisposeQueued;

[EditorBrowsable(EditorBrowsableState.Never)]
protected internal bool IsDisposedOrQueued => disposed || isDisposeQueued;

static private void ClearHolder(IntPtr handle)
{
nativeBindedHolder.Remove(handle);
}

[Conditional("NUI_DISPOSE_DEBUG_ON")]
private void disposeDebuggingCtor()
{
Expand Down
Loading
Loading