From 5f9264cdaa726c1e4b0ad65450ed6c9a5dd18466 Mon Sep 17 00:00:00 2001 From: Shatyuka <958182453@qq.com> Date: Fri, 1 Mar 2024 04:33:48 +0800 Subject: [PATCH 1/2] Fix `destroy_gpu_instance` crash --- src/gpu.cpp | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/src/gpu.cpp b/src/gpu.cpp index 1dad6cdb01a..2b4434ceaec 100644 --- a/src/gpu.cpp +++ b/src/gpu.cpp @@ -51,6 +51,7 @@ class __ncnn_vulkan_instance_holder { instance = 0; created = 0; + glslang_initialized = false; #if NCNN_VULKAN_LOADER libvulkan = 0; @@ -76,6 +77,7 @@ class __ncnn_vulkan_instance_holder VkInstance instance; int created; + bool glslang_initialized; #if ENABLE_VALIDATION_LAYER VkDebugUtilsMessengerEXT callback; @@ -2061,7 +2063,7 @@ int create_gpu_instance(const char* driver_path) // the default gpu device g_default_gpu_index = find_default_vulkan_device_index(); - glslang::InitializeProcess(); + g_instance.glslang_initialized = glslang::InitializeProcess(); // the global __ncnn_vulkan_instance_holder destructor will call destroy_gpu_instance() on exit // but it seems to be too late for nvidia driver :( @@ -2091,7 +2093,8 @@ void destroy_gpu_instance() // NCNN_LOGE("destroy_gpu_instance"); - glslang::FinalizeProcess(); + if (g_instance.glslang_initialized) + glslang::FinalizeProcess(); for (int i = 0; i < NCNN_MAX_GPU_COUNT; i++) { @@ -2110,7 +2113,8 @@ void destroy_gpu_instance() } #endif // ENABLE_VALIDATION_LAYER - vkDestroyInstance(g_instance, 0); + if (vkDestroyInstance) + vkDestroyInstance(g_instance, 0); g_instance.instance = 0; From 91ed90bb13fec9b74053d0b4d7f627971a1540b8 Mon Sep 17 00:00:00 2001 From: shatyuka <958182453@qq.com> Date: Fri, 1 Mar 2024 17:29:39 +0800 Subject: [PATCH 2/2] Additional check and clear --- src/gpu.cpp | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/src/gpu.cpp b/src/gpu.cpp index 2b4434ceaec..6daf45c8512 100644 --- a/src/gpu.cpp +++ b/src/gpu.cpp @@ -2094,7 +2094,10 @@ void destroy_gpu_instance() // NCNN_LOGE("destroy_gpu_instance"); if (g_instance.glslang_initialized) + { glslang::FinalizeProcess(); + g_instance.glslang_initialized = false; + } for (int i = 0; i < NCNN_MAX_GPU_COUNT; i++) { @@ -2106,7 +2109,7 @@ void destroy_gpu_instance() } #if ENABLE_VALIDATION_LAYER - if (support_VK_EXT_debug_utils) + if (support_VK_EXT_debug_utils && g_instance.callback) { DestroyDebugUtilsMessengerEXT(g_instance, g_instance.callback, NULL); g_instance.callback = 0; @@ -2114,7 +2117,10 @@ void destroy_gpu_instance() #endif // ENABLE_VALIDATION_LAYER if (vkDestroyInstance) + { vkDestroyInstance(g_instance, 0); + vkDestroyInstance = 0; + } g_instance.instance = 0;