vkCreateInstance抛出分段错误

文化网编2023-04-08 08:101940
提问开始:

我正在努力学习一些伏尔坎语言,我对vkCreateInstance有一个奇怪的问题。它似乎抛出分割错误,但仅在非常特定的情况下。为了更清楚,这里有一些代码:

int main() {
    // Load Vulkan Functions, extensions and stuff
    VkApplicationInfo applicationInfo;
    applicationInfo.sType = VK_STRUCTURE_TYPE_APPLICATION_INFO;
    applicationInfo.pNext = nullptr;
    applicationInfo.pApplicationName = "VulkanDoodles";
    applicationInfo.applicationVersion = VK_MAKE_VERSION(1,0,0);
    applicationInfo.pEngineName = "EngineNamePlaceholder";
    applicationInfo.engineVersion = VK_MAKE_VERSION(1,0,0);
    applicationInfo.apiVersion = VK_MAKE_VERSION(1,0,0);

    VkInstanceCreateInfo instanceCreateInfo;
    instanceCreateInfo.sType = VK_STRUCTURE_TYPE_INSTANCE_CREATE_INFO;
    instanceCreateInfo.pNext = nullptr;
    instanceCreateInfo.flags = 0;
    instanceCreateInfo.pApplicationInfo = &applicationInfo;
    instanceCreateInfo.ppEnabledLayerNames = nullptr;
    instanceCreateInfo.enabledExtensionCount = (uint32_t)desired_extensions.size();
    instanceCreateInfo.ppEnabledExtensionNames = &desired_extensions[0];

    VkInstance instance;
    if(vk::vkCreateInstance(&instanceCreateInfo, nullptr, &instance) != VK_SUCCESS){
        std::cout << "Could not create instance" << std::endl;
        return -1;
    }
    // rest of the code
    return 0;
}

它工作得很好,实例就创建好了。因此,我决定将代码放在一个函数中以提高可读性。但是下面这样的代码不再起作用了:

VkInstance CreateInstance(const std::vector<const char*> extensions){
    VkApplicationInfo applicationInfo;
    applicationInfo.sType = VK_STRUCTURE_TYPE_APPLICATION_INFO;
    applicationInfo.pNext = nullptr;
    applicationInfo.pApplicationName = "VulkanDoodles";
    applicationInfo.applicationVersion = VK_MAKE_VERSION(1,0,0);
    applicationInfo.pEngineName = "EngineNamePlaceholder";
    applicationInfo.engineVersion = VK_MAKE_VERSION(1,0,0);
    applicationInfo.apiVersion = VK_MAKE_VERSION(1,0,0);

    VkInstanceCreateInfo instanceCreateInfo;
    instanceCreateInfo.sType = VK_STRUCTURE_TYPE_INSTANCE_CREATE_INFO;
    instanceCreateInfo.pNext = nullptr;
    instanceCreateInfo.flags = 0;
    instanceCreateInfo.pApplicationInfo = &applicationInfo;
    instanceCreateInfo.ppEnabledLayerNames = nullptr;
    instanceCreateInfo.enabledExtensionCount = (uint32_t)extensions.size();
    instanceCreateInfo.ppEnabledExtensionNames = &extensions[0];
    VkInstance instance;
    if(vk::vkCreateInstance(&instanceCreateInfo, nullptr, &instance) != VK_SUCCESS){
        std::cout << "Could not create instance" << std::endl;
    }
    return instance;
}

int main(){
    // Load Vulkan Functions, extensions and stuff
    VkInstance instance = CreateInstance(desired_extensions);
    // rest of the code
}

vkCreateInstance的调用抛出了分段错误。考虑到下一个代码片段可以正常工作,扩展向量似乎不是问题所在:

VkInstanceCreateInfo CreateInstanceInfo(const std::vector<const char*>& extensions){
    VkApplicationInfo applicationInfo;
    applicationInfo.sType = VK_STRUCTURE_TYPE_APPLICATION_INFO;
    applicationInfo.pNext = nullptr;
    applicationInfo.pApplicationName = "VulkanDoodles";
    applicationInfo.applicationVersion = VK_MAKE_VERSION(1,0,0);
    applicationInfo.pEngineName = "EngineNamePlaceholder";
    applicationInfo.engineVersion = VK_MAKE_VERSION(1,0,0);
    applicationInfo.apiVersion = VK_MAKE_VERSION(1,0,0);

    VkInstanceCreateInfo instanceCreateInfo;
    instanceCreateInfo.sType = VK_STRUCTURE_TYPE_INSTANCE_CREATE_INFO;
    instanceCreateInfo.pNext = nullptr;
    instanceCreateInfo.flags = 0;
    instanceCreateInfo.pApplicationInfo = &applicationInfo;
    instanceCreateInfo.ppEnabledLayerNames = nullptr;
    instanceCreateInfo.enabledExtensionCount = (uint32_t)extensions.size();
    instanceCreateInfo.ppEnabledExtensionNames = &extensions[0];

    return instanceCreateInfo;
}

VkInstance CreateInstance(const VkInstanceCreateInfo& instanceCreateInfo){
    VkInstance instance;
    if(vk::vkCreateInstance(&instanceCreateInfo, nullptr, &instance) != VK_SUCCESS){
        std::cout << "Could not create instance" << std::endl;
    }
    return instance;
}

int main(){
    // Load Vulkan Functions, extensions and stuff
    VkInstanceCreateInfo instanceCreateInfo = CreateInstanceInfo(desired_extensions);
    VkInstance instance = CreateInstance(instanceCreateInfo);
    // rest of the code
}

如前所述,这可以很好地工作,我猜测必须在main函数中声明VkInstanceCreateInfo变量。

所以我的问题是:

为什么vkCreateInstance会抛出段错误?为什么只是偶尔呢?是不是发生了什么奇怪的事?

任何洞察力都会非常有帮助。

另外,为了记录,我使用Linux Mint和CLion与cmake版本3.13 (不确定这是否重要)

回答开始:得票数 1

您的中间代码片段缺少enabledLayerCount初始化,因此您让驱动程序执行OOBA。

您的最后一个代码片段返回instanceCreateInfo,其中当pApplicationInfovkCreateInstance使用时,它指向销毁的VkApplicationInfo。您使驱动程序取消引用指向已销毁内存的指针。

总结

以上是真正的电脑专家为你收集整理的vkCreateInstance抛出分段错误的全部内容,希望文章能够帮你解决所遇到的问题。

如果觉得真正的电脑专家网站内容还不错,欢迎将真正的电脑专家推荐给好友。

评论区