@@ -751,13 +751,15 @@ VKAPI_ATTR void VKAPI_CALL layer_vkDestroyInstance_default(VkInstance instance,
751751 std::unique_lock<std::mutex> lock {g_vulkanLock};
752752 auto * layer = Instance::retrieve (instance);
753753
754- // Layer proxy must be destroyed before the driver version
755- // so we can clean up any layer-owned resources
754+ // Save the driver function to avoid a use-after free when proxy is destroyed
755+ auto destroyInstance = layer->driver .vkDestroyInstance ;
756+
757+ // Layer proxy must be destroyed before the driver object as we use its dispatchable handle
756758 Instance::destroy (layer);
757759
758760 // Release the lock to call into the driver
759761 lock.unlock ();
760- layer-> driver . vkDestroyInstance (instance, pAllocator);
762+ destroyInstance (instance, pAllocator);
761763}
762764
763765/* See Vulkan API for documentation. */
@@ -839,13 +841,15 @@ VKAPI_ATTR void VKAPI_CALL layer_vkDestroyDevice_default(VkDevice device, const
839841 std::unique_lock<std::mutex> lock {g_vulkanLock};
840842 auto * layer = Device::retrieve (device);
841843
842- // Layer proxy must be destroyed before the driver version
843- // so we can clean up any layer-owned resources
844+ // Save the driver function to avoid a use-after free when proxy is destroyed
845+ auto destroyDevice = layer->driver .vkDestroyDevice ;
846+
847+ // Layer proxy must be destroyed before the driver object as we use its dispatchable handle
844848 Device::destroy (layer);
845849
846850 // Release the lock to call into the driver
847851 lock.unlock ();
848- layer-> driver . vkDestroyDevice (device, pAllocator);
852+ destroyDevice (device, pAllocator);
849853}
850854
851855/* See Vulkan API for documentation. */
0 commit comments