vkCreateInstance 导致分段错误 [英] vkCreateInstance causing a segmentation fault

查看:47
本文介绍了vkCreateInstance 导致分段错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我刚刚开始学习 Vulkan.我有 Graham Sellers 的书Vulkan Programming Guide",以及我的系统中带有 AMDGPU pro 驱动程序的 RX 480.我正在运行 Arch Linux,并且能够在我的系统上运行一些 Vulkan 演示.

我有一个导致分段错误的最小代码块.奇怪的是,在我为了提出这个问题而生成这个块的过程中,我确实让它在从构造函数调用 vkCreateInstance() 的情况下运行,并且在我向代码中添加 try/catch 时首先注意到了一个分段错误.

现在,即使没有 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_fault 
1
Segmentation 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 导致分段错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆