diff --git a/base/vulkanexamplebase.cpp b/base/vulkanexamplebase.cpp index abbb2f9f..a64a1905 100644 --- a/base/vulkanexamplebase.cpp +++ b/base/vulkanexamplebase.cpp @@ -1205,6 +1205,11 @@ void VulkanExampleBase::handleAppCommand(android_app * app, int32_t cmd) LOGD("APP_CMD_GAINED_FOCUS"); vulkanExample->focused = true; break; + case APP_CMD_TERM_WINDOW: + // Window is hidden or closed, clean up resources + LOGD("APP_CMD_TERM_WINDOW"); + vulkanExample->swapChain.cleanup(); + break; } } #elif defined(__linux__) diff --git a/base/vulkanswapchain.hpp b/base/vulkanswapchain.hpp index d703611b..da630861 100644 --- a/base/vulkanswapchain.hpp +++ b/base/vulkanswapchain.hpp @@ -75,14 +75,13 @@ private: public: VkFormat colorFormat; VkColorSpaceKHR colorSpace; - - VkSwapchainKHR swapChain = VK_NULL_HANDLE; - + /** @brief Handle to the current swap chain, required for recreation */ + VkSwapchainKHR swapChain = VK_NULL_HANDLE; uint32_t imageCount; std::vector images; std::vector buffers; - // Index of the deteced graphics and presenting device queue + /** @brief Queue family index of the deteced graphics and presenting device queue */ uint32_t queueNodeIndex = UINT32_MAX; // Creates an os specific surface @@ -465,12 +464,20 @@ public: */ void cleanup() { - for (uint32_t i = 0; i < imageCount; i++) + if (swapChain != VK_NULL_HANDLE) { - vkDestroyImageView(device, buffers[i].view, nullptr); + for (uint32_t i = 0; i < imageCount; i++) + { + vkDestroyImageView(device, buffers[i].view, nullptr); + } } - fpDestroySwapchainKHR(device, swapChain, nullptr); - vkDestroySurfaceKHR(instance, surface, nullptr); + if (surface != VK_NULL_HANDLE) + { + fpDestroySwapchainKHR(device, swapChain, nullptr); + vkDestroySurfaceKHR(instance, surface, nullptr); + } + surface = VK_NULL_HANDLE; + swapChain = VK_NULL_HANDLE; } };