From c9a574a764aebcadec1aadaeae66b730c102b3be Mon Sep 17 00:00:00 2001 From: Kovid Goyal Date: Thu, 4 Jul 2019 09:03:13 +0530 Subject: [PATCH] Cocoa: Ensure no callbacks are called on the view even if it is retained after being released Fixes #1761 (I hope) --- glfw/cocoa_window.m | 42 ++++++++++++++++++++++++++++++++---------- 1 file changed, 32 insertions(+), 10 deletions(-) diff --git a/glfw/cocoa_window.m b/glfw/cocoa_window.m index 26756e5b3..f05b4fe9c 100644 --- a/glfw/cocoa_window.m +++ b/glfw/cocoa_window.m @@ -649,13 +649,18 @@ - (void)dealloc [super dealloc]; } +- (void) removeGLFWWindow +{ + window = NULL; +} + - (_GLFWwindow*)glfwWindow { return window; } - (BOOL)isOpaque { - return [window->ns.object isOpaque]; + return window && [window->ns.object isOpaque]; } - (BOOL)canBecomeKeyView @@ -670,11 +675,13 @@ - (BOOL)acceptsFirstResponder - (void) viewWillStartLiveResize { + if (!window) return; _glfwInputLiveResize(window, true); } - (void)viewDidEndLiveResize { + if (!window) return; _glfwInputLiveResize(window, false); } @@ -685,6 +692,7 @@ - (BOOL)wantsUpdateLayer - (void)updateLayer { + if (!window) return; if (window->context.client != GLFW_NO_API) { @try { [window->context.nsgl.object update]; @@ -701,7 +709,7 @@ - (void)updateLayer - (void)cursorUpdate:(NSEvent *)event { (void)event; - updateCursorImage(window); + if (window) updateCursorImage(window); } - (BOOL)acceptsFirstMouse:(NSEvent *)event @@ -712,10 +720,12 @@ - (BOOL)acceptsFirstMouse:(NSEvent *)event - (void)mouseDown:(NSEvent *)event { - _glfwInputMouseClick(window, - GLFW_MOUSE_BUTTON_LEFT, - GLFW_PRESS, - translateFlags([event modifierFlags])); + if (!window) return; + _glfwInputMouseClick( + window, + GLFW_MOUSE_BUTTON_LEFT, + GLFW_PRESS, + translateFlags([event modifierFlags])); } - (void)mouseDragged:(NSEvent *)event @@ -725,14 +735,17 @@ - (void)mouseDragged:(NSEvent *)event - (void)mouseUp:(NSEvent *)event { - _glfwInputMouseClick(window, - GLFW_MOUSE_BUTTON_LEFT, - GLFW_RELEASE, - translateFlags([event modifierFlags])); + if (!window) return; + _glfwInputMouseClick( + window, + GLFW_MOUSE_BUTTON_LEFT, + GLFW_RELEASE, + translateFlags([event modifierFlags])); } - (void)mouseMoved:(NSEvent *)event { + if (!window) return; if (window->cursorMode == GLFW_CURSOR_DISABLED) { const double dx = [event deltaX] - window->ns.cursorWarpDeltaX; @@ -757,6 +770,7 @@ - (void)mouseMoved:(NSEvent *)event - (void)rightMouseDown:(NSEvent *)event { + if (!window) return; _glfwInputMouseClick(window, GLFW_MOUSE_BUTTON_RIGHT, GLFW_PRESS, @@ -770,6 +784,7 @@ - (void)rightMouseDragged:(NSEvent *)event - (void)rightMouseUp:(NSEvent *)event { + if (!window) return; _glfwInputMouseClick(window, GLFW_MOUSE_BUTTON_RIGHT, GLFW_RELEASE, @@ -778,6 +793,7 @@ - (void)rightMouseUp:(NSEvent *)event - (void)otherMouseDown:(NSEvent *)event { + if (!window) return; _glfwInputMouseClick(window, (int) [event buttonNumber], GLFW_PRESS, @@ -791,6 +807,7 @@ - (void)otherMouseDragged:(NSEvent *)event - (void)otherMouseUp:(NSEvent *)event { + if (!window) return; _glfwInputMouseClick(window, (int) [event buttonNumber], GLFW_RELEASE, @@ -800,17 +817,20 @@ - (void)otherMouseUp:(NSEvent *)event - (void)mouseExited:(NSEvent *)event { (void)event; + if (!window) return; _glfwInputCursorEnter(window, false); } - (void)mouseEntered:(NSEvent *)event { (void)event; + if (!window) return; _glfwInputCursorEnter(window, true); } - (void)viewDidChangeBackingProperties { + if (!window) return; const NSRect contentRect = [window->ns.view frame]; const NSRect fbRect = [window->ns.view convertRectToBacking:contentRect]; @@ -839,6 +859,7 @@ - (void)viewDidChangeBackingProperties - (void)drawRect:(NSRect)rect { (void)rect; + if (!window) return; _glfwInputWindowDamage(window); } @@ -1530,6 +1551,7 @@ void _glfwPlatformDestroyWindow(_GLFWwindow* window) [window->ns.delegate release]; window->ns.delegate = nil; + [window->ns.view removeGLFWWindow]; [window->ns.view release]; window->ns.view = nil;