Cocoa: Ensure no callbacks are called on the view even if it is retained after being released

Fixes #1761 (I hope)
This commit is contained in:
Kovid Goyal 2019-07-04 09:03:13 +05:30
parent bdb633a882
commit c9a574a764
No known key found for this signature in database
GPG Key ID: 06BC317B515ACE7C

View File

@ -649,13 +649,18 @@ static GLFWapplicationshouldhandlereopenfun handle_reopen_callback = NULL;
[super dealloc]; [super dealloc];
} }
- (void) removeGLFWWindow
{
window = NULL;
}
- (_GLFWwindow*)glfwWindow { - (_GLFWwindow*)glfwWindow {
return window; return window;
} }
- (BOOL)isOpaque - (BOOL)isOpaque
{ {
return [window->ns.object isOpaque]; return window && [window->ns.object isOpaque];
} }
- (BOOL)canBecomeKeyView - (BOOL)canBecomeKeyView
@ -670,11 +675,13 @@ static GLFWapplicationshouldhandlereopenfun handle_reopen_callback = NULL;
- (void) viewWillStartLiveResize - (void) viewWillStartLiveResize
{ {
if (!window) return;
_glfwInputLiveResize(window, true); _glfwInputLiveResize(window, true);
} }
- (void)viewDidEndLiveResize - (void)viewDidEndLiveResize
{ {
if (!window) return;
_glfwInputLiveResize(window, false); _glfwInputLiveResize(window, false);
} }
@ -685,6 +692,7 @@ static GLFWapplicationshouldhandlereopenfun handle_reopen_callback = NULL;
- (void)updateLayer - (void)updateLayer
{ {
if (!window) return;
if (window->context.client != GLFW_NO_API) { if (window->context.client != GLFW_NO_API) {
@try { @try {
[window->context.nsgl.object update]; [window->context.nsgl.object update];
@ -701,7 +709,7 @@ static GLFWapplicationshouldhandlereopenfun handle_reopen_callback = NULL;
- (void)cursorUpdate:(NSEvent *)event - (void)cursorUpdate:(NSEvent *)event
{ {
(void)event; (void)event;
updateCursorImage(window); if (window) updateCursorImage(window);
} }
- (BOOL)acceptsFirstMouse:(NSEvent *)event - (BOOL)acceptsFirstMouse:(NSEvent *)event
@ -712,7 +720,9 @@ static GLFWapplicationshouldhandlereopenfun handle_reopen_callback = NULL;
- (void)mouseDown:(NSEvent *)event - (void)mouseDown:(NSEvent *)event
{ {
_glfwInputMouseClick(window, if (!window) return;
_glfwInputMouseClick(
window,
GLFW_MOUSE_BUTTON_LEFT, GLFW_MOUSE_BUTTON_LEFT,
GLFW_PRESS, GLFW_PRESS,
translateFlags([event modifierFlags])); translateFlags([event modifierFlags]));
@ -725,7 +735,9 @@ static GLFWapplicationshouldhandlereopenfun handle_reopen_callback = NULL;
- (void)mouseUp:(NSEvent *)event - (void)mouseUp:(NSEvent *)event
{ {
_glfwInputMouseClick(window, if (!window) return;
_glfwInputMouseClick(
window,
GLFW_MOUSE_BUTTON_LEFT, GLFW_MOUSE_BUTTON_LEFT,
GLFW_RELEASE, GLFW_RELEASE,
translateFlags([event modifierFlags])); translateFlags([event modifierFlags]));
@ -733,6 +745,7 @@ static GLFWapplicationshouldhandlereopenfun handle_reopen_callback = NULL;
- (void)mouseMoved:(NSEvent *)event - (void)mouseMoved:(NSEvent *)event
{ {
if (!window) return;
if (window->cursorMode == GLFW_CURSOR_DISABLED) if (window->cursorMode == GLFW_CURSOR_DISABLED)
{ {
const double dx = [event deltaX] - window->ns.cursorWarpDeltaX; const double dx = [event deltaX] - window->ns.cursorWarpDeltaX;
@ -757,6 +770,7 @@ static GLFWapplicationshouldhandlereopenfun handle_reopen_callback = NULL;
- (void)rightMouseDown:(NSEvent *)event - (void)rightMouseDown:(NSEvent *)event
{ {
if (!window) return;
_glfwInputMouseClick(window, _glfwInputMouseClick(window,
GLFW_MOUSE_BUTTON_RIGHT, GLFW_MOUSE_BUTTON_RIGHT,
GLFW_PRESS, GLFW_PRESS,
@ -770,6 +784,7 @@ static GLFWapplicationshouldhandlereopenfun handle_reopen_callback = NULL;
- (void)rightMouseUp:(NSEvent *)event - (void)rightMouseUp:(NSEvent *)event
{ {
if (!window) return;
_glfwInputMouseClick(window, _glfwInputMouseClick(window,
GLFW_MOUSE_BUTTON_RIGHT, GLFW_MOUSE_BUTTON_RIGHT,
GLFW_RELEASE, GLFW_RELEASE,
@ -778,6 +793,7 @@ static GLFWapplicationshouldhandlereopenfun handle_reopen_callback = NULL;
- (void)otherMouseDown:(NSEvent *)event - (void)otherMouseDown:(NSEvent *)event
{ {
if (!window) return;
_glfwInputMouseClick(window, _glfwInputMouseClick(window,
(int) [event buttonNumber], (int) [event buttonNumber],
GLFW_PRESS, GLFW_PRESS,
@ -791,6 +807,7 @@ static GLFWapplicationshouldhandlereopenfun handle_reopen_callback = NULL;
- (void)otherMouseUp:(NSEvent *)event - (void)otherMouseUp:(NSEvent *)event
{ {
if (!window) return;
_glfwInputMouseClick(window, _glfwInputMouseClick(window,
(int) [event buttonNumber], (int) [event buttonNumber],
GLFW_RELEASE, GLFW_RELEASE,
@ -800,17 +817,20 @@ static GLFWapplicationshouldhandlereopenfun handle_reopen_callback = NULL;
- (void)mouseExited:(NSEvent *)event - (void)mouseExited:(NSEvent *)event
{ {
(void)event; (void)event;
if (!window) return;
_glfwInputCursorEnter(window, false); _glfwInputCursorEnter(window, false);
} }
- (void)mouseEntered:(NSEvent *)event - (void)mouseEntered:(NSEvent *)event
{ {
(void)event; (void)event;
if (!window) return;
_glfwInputCursorEnter(window, true); _glfwInputCursorEnter(window, true);
} }
- (void)viewDidChangeBackingProperties - (void)viewDidChangeBackingProperties
{ {
if (!window) return;
const NSRect contentRect = [window->ns.view frame]; const NSRect contentRect = [window->ns.view frame];
const NSRect fbRect = [window->ns.view convertRectToBacking:contentRect]; const NSRect fbRect = [window->ns.view convertRectToBacking:contentRect];
@ -839,6 +859,7 @@ static GLFWapplicationshouldhandlereopenfun handle_reopen_callback = NULL;
- (void)drawRect:(NSRect)rect - (void)drawRect:(NSRect)rect
{ {
(void)rect; (void)rect;
if (!window) return;
_glfwInputWindowDamage(window); _glfwInputWindowDamage(window);
} }
@ -1530,6 +1551,7 @@ void _glfwPlatformDestroyWindow(_GLFWwindow* window)
[window->ns.delegate release]; [window->ns.delegate release];
window->ns.delegate = nil; window->ns.delegate = nil;
[window->ns.view removeGLFWWindow];
[window->ns.view release]; [window->ns.view release];
window->ns.view = nil; window->ns.view = nil;