现在,即使没有 try/catch 这也会导致分段错误:#include #include int main(int argv, char* argc[]){VkInstance* 实例;VkApplicationInfo appInfo = { .sType = VK_STRUCTURE_TYPE_APPLICATION_INFO,.pNext = NULL,.pApplicationName = "步骤 1",.applicationVersion = 1,.pEngineName = NULL,.engineVersion = 0,.apiVersion = VK_MAKE_VERSION(1, 0, 26) };//这就是vulkanCapsViewer所说的我的API版本.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 结果 = vkCreateInstance(&createInfo, NULL, instance);std::cout <<"2\n";if(result != VK_SUCCESS) std::cout <<"未能创建 Vulkan 实例:" <<结果输出为:93>./create_seg_fault1分段错误(核心转储) 解决方案 vkCreateInstance 期望指向它将填充的已分配对象的指针,您只是给它指向无处的指针(在调试中可能为 0,在发布时将是垃圾),以测试它 - 创建对象一个堆栈并给出它的地址: VkInstance 实例;...VkResult 结果 = vkCreateInstance(&createInfo, NULL, &instance);但请记住,一旦您的函数的作用域结束(在本例中为 main),该对象就会消亡.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_fault1Segmentation fault (core dumped) 解决方案 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). 这篇关于vkCreateInstance 导致分段错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 上岸,阿里云!
05-31 01:19