简体   繁体   English

为什么从方法获取vulkan扩展会使vulkan实例创建失败?

[英]why does getting vulkan extensions from a method fail vulkan instance creation?

I am following the vulkan-tutorial.com tutorial and im on the validation layers step. 我正在关注vulkan-tutorial.com教程,并在验证层步骤中关注即时消息。 In the tutorial Alexander Overvoorde the author moved the gathering of available extensions for instance creation to its own function. 在教程Alexander Overvoorde中,作者将可用扩展的集合(例如用于实例创建)移到了自己的函数中。

std::vector<const char*> getRequiredExtensions()

Previously i had gathered that info a little differently because i am using SDL2 instead of glfw, but my program ran with instance creation and no validation errors. 以前,我收集该信息的方式有所不同,因为我使用的是SDL2而不是glfw,但是我的程序运行时实例创建且没有验证错误。 Problem is when i moved the code to this function i can no longer get instance creation. 问题是,当我将代码移至该函数时,无法再创建实例。

this worked fine: 这工作正常:

unsigned int extensionCount = 0;
vkEnumerateInstanceExtensionProperties(NULL, &extensionCount, NULL);
std::vector<VkExtensionProperties> extensionProperties(extensionCount);
vkEnumerateInstanceExtensionProperties(NULL, &extensionCount, extensionProperties.data());
std::vector<const char*> extensionNames;
std::cout << "available extensions:" << std::endl;
int i = 0;
while (i < extensionCount) {
    extensionNames.push_back(extensionProperties[i].extensionName);
    std::cout << extensionNames[i] << std::endl;
    i++;
}
if (enableValidationLayers) {
    extensionNames.push_back(VK_EXT_DEBUG_REPORT_EXTENSION_NAME);
}*/
createInfo.enabledExtensionCount = extensionCount;
createInfo.ppEnabledExtensionNames = extensionNames.data();

but this failed even though the function uses the exact same code and returns extensionNames, i then use it like this: 但这即使函数使用完全相同的代码并返回extensionNames也会失败,然后我像这样使用它:

std::vector<const char*> extensionNames = getRequiredExtensions();
createInfo.enabledExtensionCount = static_cast<uint32_t>(extensionNames.size());
createInfo.ppEnabledExtensionNames = extensionNames.data();

So why doesn't this work? 那为什么不起作用呢? I have a formally taught background of java but have coded in c++ for a year, so it is possibly like a syntax error or pointer that i am sending wrong. 我有Java的正式授课背景,但是已经用C ++编写了一年,所以可能像语法错误或指针发送错误一样。 Additionally 另外

reateInfo.enabledExtensionCount = static_cast<uint32_t>(getRequiredExtensions().size());

works just fine so the returned vector is of the correct size: 5 i believe because i have 4 extensions plus the debug one. 工作正常,所以返回的向量大小正确:5我相信是因为我有4个扩展名加上调试一个。

From the documentation at Khronos.org , VkExtensionProperties looks like this: Khronos.org的文档中VkExtensionProperties看起来像这样:

typedef struct VkExtensionProperties {
    char        extensionName[VK_MAX_EXTENSION_NAME_SIZE];
    uint32_t    specVersion;
} VkExtensionProperties;

... so on this line: ...在这行上:

extensionNames.push_back(extensionProperties[i].extensionName);

... what you're storing into extensionNames are pointers to arrays that live in extensionProperties , which is local to your function. ...您要存储到extensionNames是指向存在于extensionProperties数组的指针,该数组对于函数而言是本地的。 When you return from the function, all of the arrays are destructed along with extensionProperties , and all of your pointers are now dangling. 从函数返回时,所有数组和extensionProperties都被破坏,并且所有指针现在都悬空了。
What you get then is undefined behaviour when Vulkan tries to use these dead pointers. 当Vulkan尝试使用这些死指针时,您得到的是未定义的行为。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM