SDL: Fixed crash when disconnecting external display on iOS

From 753f3f860938ae2a02fe7a7c31c00d73b37ac2c2 Mon Sep 17 00:00:00 2001
From: Sam Lantinga <[EMAIL REDACTED]>
Date: Fri, 5 Jul 2024 15:51:20 -0700
Subject: [PATCH] Fixed crash when disconnecting external display on iOS

---
 src/video/uikit/SDL_uikitmodes.h | 2 +-
 src/video/uikit/SDL_uikitmodes.m | 7 ++++---
 2 files changed, 5 insertions(+), 4 deletions(-)

diff --git a/src/video/uikit/SDL_uikitmodes.h b/src/video/uikit/SDL_uikitmodes.h
index e8f4b0371c6b8..7fa01d6c3c494 100644
--- a/src/video/uikit/SDL_uikitmodes.h
+++ b/src/video/uikit/SDL_uikitmodes.h
@@ -48,7 +48,7 @@ extern SDL_bool UIKit_IsDisplayLandscape(UIScreen *uiscreen);
 extern int UIKit_InitModes(SDL_VideoDevice *_this);
 #ifndef SDL_PLATFORM_VISIONOS
 extern int UIKit_AddDisplay(UIScreen *uiscreen, SDL_bool send_event);
-extern void UIKit_DelDisplay(UIScreen *uiscreen);
+extern void UIKit_DelDisplay(UIScreen *uiscreen, SDL_bool send_event);
 #endif
 extern int UIKit_GetDisplayModes(SDL_VideoDevice *_this, SDL_VideoDisplay *display);
 extern int UIKit_SetDisplayMode(SDL_VideoDevice *_this, SDL_VideoDisplay *display, SDL_DisplayMode *mode);
diff --git a/src/video/uikit/SDL_uikitmodes.m b/src/video/uikit/SDL_uikitmodes.m
index 8b8ada13db53c..98b84ae96619e 100644
--- a/src/video/uikit/SDL_uikitmodes.m
+++ b/src/video/uikit/SDL_uikitmodes.m
@@ -92,7 +92,7 @@ + (void)screenConnected:(NSNotification *)notification
 + (void)screenDisconnected:(NSNotification *)notification
 {
     UIScreen *uiscreen = [notification object];
-    UIKit_DelDisplay(uiscreen);
+    UIKit_DelDisplay(uiscreen, SDL_TRUE);
 }
 
 @end
@@ -308,7 +308,7 @@ int UIKit_AddDisplay(SDL_bool send_event){
 
 #ifndef SDL_PLATFORM_VISIONOS
 
-void UIKit_DelDisplay(UIScreen *uiscreen)
+void UIKit_DelDisplay(UIScreen *uiscreen, SDL_bool send_event)
 {
     SDL_DisplayID *displays;
     int i;
@@ -321,7 +321,8 @@ void UIKit_DelDisplay(UIScreen *uiscreen)
 
             if (data && data.uiscreen == uiscreen) {
                 CFRelease(display->driverdata);
-                SDL_DelVideoDisplay(displays[i], SDL_FALSE);
+                display->driverdata = NULL;
+                SDL_DelVideoDisplay(displays[i], send_event);
                 break;
             }
         }