From 8f1c387c9a8a0b8204741c574f0969e5ea877fc1 Mon Sep 17 00:00:00 2001 From: Vsevolod Kukol Date: Tue, 8 Jan 2019 13:34:46 +0100 Subject: [PATCH] [Mac] Fix ref count bug when disposing popups Track XamMac fix 49da6b7a92c4391e0d96c3948becfd22ab6c8daa for windows in PopupWindowBackend --- Xwt.XamMac/Xwt.Mac/PopupWindowBackend.cs | 25 +++++++++++++----------- Xwt.XamMac/Xwt.Mac/WindowBackend.cs | 2 +- 2 files changed, 15 insertions(+), 12 deletions(-) diff --git a/Xwt.XamMac/Xwt.Mac/PopupWindowBackend.cs b/Xwt.XamMac/Xwt.Mac/PopupWindowBackend.cs index 10ae823e8..73a28c87c 100644 --- a/Xwt.XamMac/Xwt.Mac/PopupWindowBackend.cs +++ b/Xwt.XamMac/Xwt.Mac/PopupWindowBackend.cs @@ -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) diff --git a/Xwt.XamMac/Xwt.Mac/WindowBackend.cs b/Xwt.XamMac/Xwt.Mac/WindowBackend.cs index a011199af..f070c2751 100644 --- a/Xwt.XamMac/Xwt.Mac/WindowBackend.cs +++ b/Xwt.XamMac/Xwt.Mac/WindowBackend.cs @@ -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;