我正在尝试学习一些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一起使用(不确定是否重要)
最佳答案
您的中间代码段缺少enabledLayerCount
初始化,因此使驱动程序执行OOBA。
您的最后一个代码段返回instanceCreateInfo
,其中pApplicationInfo
在被VkApplicationInfo
使用时指向被破坏的vkCreateInstance
。您使驱动程序取消引用指针以破坏内存。
关于c++ - vkCreateInstance引发段错误,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/54841772/