From 0e31e4705a2386f0f15f1ec6b9e58596ddfed07d Mon Sep 17 00:00:00 2001 From: Benny Sjoestrand Date: Sun, 12 Oct 2025 11:25:21 +0200 Subject: [PATCH] Fix crash, when a TextureView referenced by a framebuffer is deleted In VulkanResourceManager::createRenderPassCommandRecorder Vulkan framebuffers are created on demand. Then if a TextureView associated with an exisiting Vulkan framebuffer is deleted it the application will crash. Keep track what framebuffers are associated with the TextureView, and when it's deleted also cleanup the device framebuffers. --- src/KDGpu/vulkan/vulkan_resource_manager.cpp | 17 +++++++++++++++++ src/KDGpu/vulkan/vulkan_texture_view.h | 3 +++ 2 files changed, 20 insertions(+) diff --git a/src/KDGpu/vulkan/vulkan_resource_manager.cpp b/src/KDGpu/vulkan/vulkan_resource_manager.cpp index 535d7ea0..19d8ea41 100644 --- a/src/KDGpu/vulkan/vulkan_resource_manager.cpp +++ b/src/KDGpu/vulkan/vulkan_resource_manager.cpp @@ -1037,6 +1037,20 @@ void VulkanResourceManager::deleteTextureView(const Handle &handl { VulkanTextureView *vulkanTextureView = m_textureViews.get(handle); VulkanDevice *vulkanDevice = m_devices.get(vulkanTextureView->deviceHandle); + + for(const auto &vulkanFramebufferKey : vulkanTextureView->associatedFrameBuffers) { + auto itFramebuffer = vulkanDevice->framebuffers.find(vulkanFramebufferKey); + if (itFramebuffer == vulkanDevice->framebuffers.end()) { + // This framebuffer has already been destroyed, just ignore + continue; + } + + auto *framebuffer = m_framebuffers.get(itFramebuffer->second); + vkDestroyFramebuffer(vulkanDevice->device, framebuffer->framebuffer, nullptr); + vulkanDevice->framebuffers.erase(itFramebuffer); + m_framebuffers.remove(itFramebuffer->second); + } + vkDestroyImageView(vulkanDevice->device, vulkanTextureView->imageView, nullptr); m_textureViews.remove(handle); @@ -2918,6 +2932,9 @@ Handle VulkanResourceManager::createFramebuffer(const HandleassociatedFrameBuffers.push_back(frameBufferKey); + const auto vulkanFramebufferHandle = m_framebuffers.emplace(VulkanFramebuffer(vkFramebuffer)); return vulkanFramebufferHandle; } diff --git a/src/KDGpu/vulkan/vulkan_texture_view.h b/src/KDGpu/vulkan/vulkan_texture_view.h index e6a6943d..d4249f34 100644 --- a/src/KDGpu/vulkan/vulkan_texture_view.h +++ b/src/KDGpu/vulkan/vulkan_texture_view.h @@ -15,6 +15,8 @@ #include +#include "vulkan_framebuffer.h" + namespace KDGpu { struct Texture_t; @@ -33,6 +35,7 @@ struct KDGPU_EXPORT VulkanTextureView { VkImageView imageView{ VK_NULL_HANDLE }; Handle textureHandle; Handle deviceHandle; + std::vector associatedFrameBuffers; }; } // namespace KDGpu