Reputation: 4915
I encountered a weird issue. I can use vkEnumerateInstanceVersion
from vulkan.h
, but using vk::enumerateInstanceVersion()
from vulkan.hpp
causes an memory access violation
(segfault).
I use this function to create the vk::Instance
:
static vk::Instance createInstance()
{
loadVulkanGlobalFunctions();
uint32_t version;
vkEnumerateInstanceVersion(&version); // works fine, returns version 1.2.131
version = vk::enumerateInstanceVersion(); // error 0xC0000005 memory access violation
std::cout << "Supported Vulkan Version is "
<< VK_VERSION_MAJOR(version) << '.'
<< VK_VERSION_MINOR(version) << '.'
<< VK_VERSION_PATCH(version) << '\n';
// instance creation ...
}
I load global Vulkan functions using the default dynamic loader:
static void loadVulkanGlobalFunctions()
{
vk::DynamicLoader loader;
auto vkGetInstanceProcAddr = loader.getProcAddress<PFN_vkGetInstanceProcAddr>("vkGetInstanceProcAddr");
VULKAN_HPP_DEFAULT_DISPATCHER.init(vkGetInstanceProcAddr);
}
I #define VULKAN_HPP_DISPATCH_LOADER_DYNAMIC 1
before #include <vulkan/vulkan.hpp>
and I have VULKAN_HPP_DEFAULT_DISPATCH_LOADER_DYNAMIC_STORAGE
exactly once in my code.
Upvotes: 1
Views: 855
Reputation: 4915
It turned out that the vk::DynamicLoader
object created in loadVulkanGlobalFunctions
must have its lifetime extended, as it unloads the dynamic library in its destructor. As it is a local variable that happens when loadVulkanGlobalFunctions
exits and the stack unwinds.
I simply changed it to this:
static vk::DynamicLoader createVulkanLoader()
{
vk::DynamicLoader loader;
auto vkGetInstanceProcAddr = loader.getProcAddress<PFN_vkGetInstanceProcAddr>("vkGetInstanceProcAddr");
VULKAN_HPP_DEFAULT_DISPATCHER.init(vkGetInstanceProcAddr);
return loader;
}
And assigned the result to a variable, whose lifetime is longer than of the vkInstance
.
Upvotes: 1