Browse Source

macOS: Fix reference counts of internal window data.

Fixes crashes when destroying or recreating a window (#5664).
Alex Szpakowski 2 years ago
parent
commit
e9c7b5191c
2 changed files with 10 additions and 1 deletions
  1. 4 1
      src/video/cocoa/SDL_cocoawindow.h
  2. 6 0
      src/video/cocoa/SDL_cocoawindow.m

+ 4 - 1
src/video/cocoa/SDL_cocoawindow.h

@@ -40,7 +40,10 @@ typedef enum
 } PendingWindowOperation;
 
 @interface Cocoa_WindowListener : NSResponder <NSWindowDelegate> {
-    SDL_WindowData *_data;
+    /* SDL_WindowData owns this Listener and has a strong reference to it.
+     * To avoid reference cycles, we could have either a weak or an
+     * unretained ref to the WindowData. */
+    __weak SDL_WindowData *_data;
     BOOL observingVisible;
     BOOL wasCtrlLeft;
     BOOL wasVisible;

+ 6 - 0
src/video/cocoa/SDL_cocoawindow.m

@@ -1653,6 +1653,12 @@ SetupWindowData(_THIS, SDL_Window * window, NSWindow *nswindow, NSView *nsview,
         SDL_SetKeyboardFocus(data.window);
     }
 
+    /* SDL_WindowData will be holding a strong reference to the NSWindow, and
+     * it will also call [NSWindow close] in DestroyWindow before releasing the
+     * NSWindow, so the extra release provided by releasedWhenClosed isn't
+     * necessary. */
+    nswindow.releasedWhenClosed = NO;
+
     /* Prevents the window's "window device" from being destroyed when it is
      * hidden. See http://www.mikeash.com/pyblog/nsopenglcontext-and-one-shot.html
      */