Skip to content

Commit

Permalink
Fix destroy_gpu_instance crash (#5353)
Browse files Browse the repository at this point in the history
* Fix `destroy_gpu_instance` crash

* Additional check and clear
  • Loading branch information
shatyuka committed Mar 4, 2024
1 parent cf293ec commit e7748e5
Showing 1 changed file with 14 additions and 4 deletions.
18 changes: 14 additions & 4 deletions src/gpu.cpp
Expand Up @@ -51,6 +51,7 @@ class __ncnn_vulkan_instance_holder
{
instance = 0;
created = 0;
glslang_initialized = false;

#if NCNN_VULKAN_LOADER
libvulkan = 0;
Expand All @@ -76,6 +77,7 @@ class __ncnn_vulkan_instance_holder

VkInstance instance;
int created;
bool glslang_initialized;

#if ENABLE_VALIDATION_LAYER
VkDebugUtilsMessengerEXT callback;
Expand Down Expand Up @@ -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 :(
Expand Down Expand Up @@ -2091,7 +2093,11 @@ void destroy_gpu_instance()

// NCNN_LOGE("destroy_gpu_instance");

glslang::FinalizeProcess();
if (g_instance.glslang_initialized)
{
glslang::FinalizeProcess();
g_instance.glslang_initialized = false;
}

for (int i = 0; i < NCNN_MAX_GPU_COUNT; i++)
{
Expand All @@ -2103,14 +2109,18 @@ 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;
}
#endif // ENABLE_VALIDATION_LAYER

vkDestroyInstance(g_instance, 0);
if (vkDestroyInstance)
{
vkDestroyInstance(g_instance, 0);
vkDestroyInstance = 0;
}

g_instance.instance = 0;

Expand Down

0 comments on commit e7748e5

Please sign in to comment.