From e5731f9bac32a8b2c92fb5659af7ece0c1ca0bce Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Arnoldo=20Adona=C3=AD=20Bar=C3=B3n=20Robles?=
<adonai_1111@hotmail.com>
Date: Wed, 10 Dec 2025 10:27:13 -0600
Subject: [PATCH] GPU: Fix crash on Android upon returning from the background
---
src/gpu/vulkan/SDL_gpu_vulkan.c | 36 ++++++++++++++++++++++++---------
1 file changed, 26 insertions(+), 10 deletions(-)
diff --git a/src/gpu/vulkan/SDL_gpu_vulkan.c b/src/gpu/vulkan/SDL_gpu_vulkan.c
index a8beda62c1a27..81c48222c9c21 100644
--- a/src/gpu/vulkan/SDL_gpu_vulkan.c
+++ b/src/gpu/vulkan/SDL_gpu_vulkan.c
@@ -4363,7 +4363,9 @@ static bool VULKAN_INTERNAL_QuerySwapchainSupport(
&supportsPresent);
// Initialize these in case anything fails
+ outputDetails->formats = NULL;
outputDetails->formatsLength = 0;
+ outputDetails->presentModes = NULL;
outputDetails->presentModesLength = 0;
if (!supportsPresent) {
@@ -4386,22 +4388,33 @@ static bool VULKAN_INTERNAL_QuerySwapchainSupport(
surface,
&outputDetails->formatsLength,
NULL);
- CHECK_VULKAN_ERROR_AND_RETURN(result, vkGetPhysicalDeviceSurfaceFormatsKHR, false);
+ if (result != VK_SUCCESS) {
+ // Make sure the driver didn't mess up this value.
+ outputDetails->formatsLength = 0;
+ CHECK_VULKAN_ERROR_AND_RETURN(result, vkGetPhysicalDeviceSurfaceFormatsKHR, false);
+ }
result = renderer->vkGetPhysicalDeviceSurfacePresentModesKHR(
physicalDevice,
surface,
&outputDetails->presentModesLength,
NULL);
- CHECK_VULKAN_ERROR_AND_RETURN(result, vkGetPhysicalDeviceSurfacePresentModesKHR, false);
+ if (result != VK_SUCCESS) {
+ // Make sure the driver didn't mess up this value.
+ outputDetails->presentModesLength = 0;
+ // Reset this one, too.
+ outputDetails->formatsLength = 0;
+ CHECK_VULKAN_ERROR_AND_RETURN(result, vkGetPhysicalDeviceSurfacePresentModesKHR, false);
+ }
// Generate the arrays, if applicable
- outputDetails->formats = NULL;
if (outputDetails->formatsLength != 0) {
outputDetails->formats = (VkSurfaceFormatKHR *)SDL_malloc(
sizeof(VkSurfaceFormatKHR) * outputDetails->formatsLength);
if (!outputDetails->formats) { // OOM
+ outputDetails->formatsLength = 0;
+ outputDetails->presentModesLength = 0;
return false;
}
@@ -4412,17 +4425,22 @@ static bool VULKAN_INTERNAL_QuerySwapchainSupport(
outputDetails->formats);
if (result != VK_SUCCESS) {
SDL_free(outputDetails->formats);
+ outputDetails->formats = NULL;
+ outputDetails->formatsLength = 0;
+ outputDetails->presentModesLength = 0;
CHECK_VULKAN_ERROR_AND_RETURN(result, vkGetPhysicalDeviceSurfaceFormatsKHR, false);
}
}
- outputDetails->presentModes = NULL;
if (outputDetails->presentModesLength != 0) {
outputDetails->presentModes = (VkPresentModeKHR *)SDL_malloc(
sizeof(VkPresentModeKHR) * outputDetails->presentModesLength);
if (!outputDetails->presentModes) { // OOM
SDL_free(outputDetails->formats);
+ outputDetails->formats = NULL;
+ outputDetails->formatsLength = 0;
+ outputDetails->presentModesLength = 0;
return false;
}
@@ -4434,6 +4452,10 @@ static bool VULKAN_INTERNAL_QuerySwapchainSupport(
if (result != VK_SUCCESS) {
SDL_free(outputDetails->formats);
SDL_free(outputDetails->presentModes);
+ outputDetails->formats = NULL;
+ outputDetails->presentModes = NULL;
+ outputDetails->formatsLength = 0;
+ outputDetails->presentModesLength = 0;
CHECK_VULKAN_ERROR_AND_RETURN(result, vkGetPhysicalDeviceSurfacePresentModesKHR, false);
}
}
@@ -4526,12 +4548,6 @@ static Uint32 VULKAN_INTERNAL_CreateSwapchain(
windowData->surface,
NULL);
windowData->surface = VK_NULL_HANDLE;
- if (swapchainSupportDetails.formatsLength > 0) {
- SDL_free(swapchainSupportDetails.formats);
- }
- if (swapchainSupportDetails.presentModesLength > 0) {
- SDL_free(swapchainSupportDetails.presentModes);
- }
return false;
}