Reputation: 35
I am following the vulkan-tutorial.com tutorial and im on the validation layers step. In the tutorial Alexander Overvoorde the author moved the gathering of available extensions for instance creation to its own function.
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. 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:
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. 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.
Upvotes: 0
Views: 1447
Reputation: 63124
From the documentation at Khronos.org, VkExtensionProperties
looks like this:
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. When you return from the function, all of the arrays are destructed along with extensionProperties
, and all of your pointers are now dangling.
What you get then is undefined behaviour when Vulkan tries to use these dead pointers.
Upvotes: 2