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

Commit

Permalink
Removes from topLevelWindows destroyed native windows to avoid dispos…
Browse files Browse the repository at this point in the history
…ed exceptions

Fixes VSTS #999372 - System.ObjectDisposedException exception in Foundation.NSObject.get_SuperHandle()
  • Loading branch information
netonjm committed Jan 22, 2020
1 parent dd8f768 commit 13ac487
Showing 1 changed file with 24 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,7 @@ enum JIS_VKS {

#if MAC
Foundation.NSObject keyMonitor;
Foundation.NSObject closeWindowMonitor;
uint throttleLastEventTime = 0;
#endif

Expand Down Expand Up @@ -124,6 +125,12 @@ public CommandManager (Window root)
ActionCommand c = new ActionCommand (CommandSystemCommands.ToolbarList, "Toolbar List", null, null, ActionType.Check);
c.CommandArray = true;
RegisterCommand (c);

closeWindowMonitor = Foundation.NSNotificationCenter.DefaultCenter.AddObserver (AppKit.NSWindow.WillCloseNotification, (s) => {
if (topLevelWindows.Any (h => h.nativeWidget == s.Object)) {
TopLevelDestroyed (s.Object, EventArgs.Empty);
}
});
}

/// <summary>
Expand Down Expand Up @@ -869,13 +876,18 @@ void TopLevelDestroyed (object o, EventArgs args)
{
RegisterUserInteraction ();

Gtk.Window w = (Gtk.Window)o;
w.Destroyed -= TopLevelDestroyed;
w.KeyPressEvent -= OnKeyPressed;
w.KeyReleaseEvent -= OnKeyReleased;
w.ButtonPressEvent -= HandleButtonPressEvent;
topLevelWindows.Remove (w);
if (o is Gtk.Window w) {
w.Destroyed -= TopLevelDestroyed;
w.KeyPressEvent -= OnKeyPressed;
w.KeyReleaseEvent -= OnKeyReleased;
w.ButtonPressEvent -= HandleButtonPressEvent;
topLevelWindows.Remove (w);
}
#if MAC
else if (o is AppKit.NSWindow nsWindow) {
topLevelWindows.Remove (nsWindow);
}

if (topLevelWindows.Count == 0) {
if (keyMonitor != null) {
AppKit.NSEvent.RemoveMonitor (keyMonitor);
Expand All @@ -884,7 +896,7 @@ void TopLevelDestroyed (object o, EventArgs args)
}
#endif

if (w == lastFocused?.nativeWidget)
if (o == lastFocused?.nativeWidget)
lastFocused = null;
}

Expand All @@ -898,6 +910,11 @@ public void Dispose ()
AppKit.NSEvent.RemoveMonitor (keyMonitor);
keyMonitor = null;
}

if (closeWindowMonitor != null) {
AppKit.NSEvent.RemoveMonitor (closeWindowMonitor);
closeWindowMonitor = null;
}
#endif

if (bindings != null) {
Expand Down

0 comments on commit 13ac487

Please sign in to comment.