问题
I'm just starting to learn Vulkan. I've got the book "Vulkan Programming Guide" by Graham Sellers, and an RX 480 with the AMDGPU pro drivers in my system. I'm running Arch Linux, and I've been able to run some Vulkan demos on my system.
I have a minimal code block which causes a segmentation fault. Oddly, on my way to generating this block in order to pose this question, I do have it running with vkCreateInstance() being called from a constructor, and first noticed a segmentation fault when I added a try/catch to my code.
Now, even without try/catch this causes a segmentation fault:
#include <iostream>
#include <vulkan/vulkan.h>
int main(int argv, char* argc[])
{
VkInstance* instance;
VkApplicationInfo appInfo = { .sType = VK_STRUCTURE_TYPE_APPLICATION_INFO,
.pNext = NULL,
.pApplicationName = "Step 1",
.applicationVersion = 1,
.pEngineName = NULL,
.engineVersion = 0,
.apiVersion = VK_MAKE_VERSION(1, 0, 26) }; //This is what vulkanCapsViewer says my API version is.
VkInstanceCreateInfo createInfo = { .sType = VK_STRUCTURE_TYPE_INSTANCE_CREATE_INFO,
.pNext = NULL,
.flags = 0 };
createInfo.pApplicationInfo = &appInfo;
createInfo.enabledExtensionCount = 0;
createInfo.ppEnabledExtensionNames = NULL;
createInfo.enabledLayerCount = 0;
createInfo.ppEnabledLayerNames = NULL;
std::cout << "1\n";
VkResult result = vkCreateInstance(&createInfo, NULL, instance);
std::cout << "2\n";
if(result != VK_SUCCESS) std::cout << "Failed to create a Vulkan instance: " << result << std::endl;
std::cout << "3\n";
return 0;
}
The output is:
93> ./create_seg_fault
1
Segmentation fault (core dumped)
回答1:
vkCreateInstance expects pointer to allocated object that it will fill in, you are giving it just pointer to nowhere (could be 0 in debug, will be garbage in release), to test it - create object on a stack and give its address:
VkInstance instance;
...
VkResult result = vkCreateInstance(&createInfo, NULL, &instance);
but bear in mind that this object will die once your function's scope ends (main
in this case).
来源:https://stackoverflow.com/questions/41604097/vkcreateinstance-causing-a-segmentation-fault