vkCreateInstance引发分段错误

时间:2019-02-23 12:46:47

标签: c++ vulkan

我正在尝试学习Vulkan,而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
}

正如之前所说的,这很好,我的猜测是VkInstanceCreateInfo变量必须在main函数内部声明。

所以我的问题是:

为什么vkCreateInstance会引发段错误?为什么只有有时候呢?那里有什么奇怪的事情发生吗?

任何见解都会很有帮助。

为了便于记录,我还将Linux Mint和CLion与cmake版本3.13一起使用(不确定是否很重要)

1 个答案:

答案 0 :(得分:1)

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

您的最后一个代码段返回instanceCreateInfo,其中pApplicationInfo指向VkApplicationInfo使用时被销毁的vkCreateInstance。您使驱动程序取消引用指针以破坏内存。