diff --git a/src/gpu.cpp b/src/gpu.cpp index 6daf45c8512..77a63f4b3be 100644 --- a/src/gpu.cpp +++ b/src/gpu.cpp @@ -2091,6 +2091,19 @@ void destroy_gpu_instance() if (g_instance.created == 0) return; + for (int i = 0; i < NCNN_MAX_GPU_COUNT; i++) + { + VulkanDevice* vulkan_device = g_default_vkdev[i]; + if (vulkan_device) + { + VkDevice vkdev = g_default_vkdev[i]->vkdevice(); + if (vkdev) + { + vkDeviceWaitIdle(vkdev); + } + } + } + // NCNN_LOGE("destroy_gpu_instance"); if (g_instance.glslang_initialized) diff --git a/src/gpu.h b/src/gpu.h index a8d7ff91fdf..4d131f71c8b 100644 --- a/src/gpu.h +++ b/src/gpu.h @@ -37,6 +37,7 @@ NCNN_EXPORT VkInstance get_gpu_instance(); // Destroy VkInstance object and free the memory of the associated object // Usually called in the destructor of the main program exit +// The function will internally ensure that all vulkan devices are idle before proceeding with destruction. NCNN_EXPORT void destroy_gpu_instance(); // vulkan core