macOS: Fix reference counts of internal window data.

Fixes crashes when destroying or recreating a window (#5664).
This commit is contained in:
Alex Szpakowski 2022-05-16 21:03:41 -03:00 committed by Sam Lantinga
parent a48004952d
commit e9c7b5191c
2 changed files with 10 additions and 1 deletions

View File

@ -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;

View File

@ -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
*/