Skip to content
This repository has been archived by the owner on Sep 4, 2024. It is now read-only.

[Mac] Fix ref count bug when disposing popups #908

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
25 changes: 14 additions & 11 deletions Xwt.XamMac/Xwt.Mac/PopupWindowBackend.cs
Original file line number Diff line number Diff line change
Expand Up @@ -546,17 +546,20 @@ protected override void Dispose(bool disposing)
{
if (VisibilityEventsEnabled() && ContentView != null)
ContentView.RemoveObserver(this, HiddenProperty);

// HACK: Xamarin.Mac/MonoMac limitation: no direct way to release a window manually
// A NSWindow instance will be removed from NSApplication.SharedApplication.Windows
// only if it is being closed with ReleasedWhenClosed set to true but not on Dispose
// and there is no managed way to tell Cocoa to release the window manually (and to
// remove it from the active window list).
// see also: https://bugzilla.xamarin.com/show_bug.cgi?id=45298
// WORKAROUND:
// bump native reference count by calling DangerousRetain()
// base.Dispose will now unref the window correctly without crashing
DangerousRetain();

if (WindowBackend.XamMacDangerousDispose)
{
// HACK: Xamarin.Mac/MonoMac limitation: no direct way to release a window manually
// A NSWindow instance will be removed from NSApplication.SharedApplication.Windows
// only if it is being closed with ReleasedWhenClosed set to true but not on Dispose
// and there is no managed way to tell Cocoa to release the window manually (and to
// remove it from the active window list).
// see also: https://bugzilla.xamarin.com/show_bug.cgi?id=45298
// WORKAROUND:
// bump native reference count by calling DangerousRetain()
// base.Dispose will now unref the window correctly without crashing
DangerousRetain();
}
// tell Cocoa to release the window on Close
ReleasedWhenClosed = true;
// Close the window (Cocoa will do its job even if the window is already closed)
Expand Down
2 changes: 1 addition & 1 deletion Xwt.XamMac/Xwt.Mac/WindowBackend.cs
Original file line number Diff line number Diff line change
Expand Up @@ -475,7 +475,7 @@ public void SetMainMenu (IMenuBackend menu)

static Selector closeSel = new Selector ("close");

static readonly bool XamMacDangerousDispose = Version.Parse(Constants.Version) < new Version(5, 6);
internal static readonly bool XamMacDangerousDispose = Version.Parse(Constants.Version) < new Version(5, 6);

bool disposing, disposed;

Expand Down