I'm building a Vulkan Project with CMake and C++. My project compiles fine under MSVC 2019. However, when calling this line:
printf("Before\n"); // The below line is where the program hangs.
const char** e = this->validationLayers->getRequiredExtensions();
this->createInfo.ppEnabledLayerNames = e;
this->createInfo.enabledLayerCount = sizeof(e) / sizeof(e[0]);
I get all the outputs for creation of my program up to the "Before" output. The next two lines don't throw a compiler error, but exit my program immediately.
Definition
const char* const* Dragon::dgVulkanValidationLayer::getRequiredExtensions() {
printf("Method");
return this->validationLayers.data();
}
Declaration
namespace Dragon {
class dgVulkanValidationLayer {
public:
...
const char* const* getRequiredExtensions();
...
private:
std::vector<const char*> validationLayers = {
"VK_LAYER_KHRONOS_validation"
};
};
};
How do I fix this issue?
My debugger exits with this line.
Exception thrown at 0x00007FF6C2880953 in Exefile.exe: 0xC0000005: Access violation reading location 0xFFFFFFFFFFFFFFFF.
EDIT: Clarification. The problem was not in how it was happening. the problem is that when calling the getRequiredExtensions()
method the program exits immediately.
EDIT 2: So after struggling with my debugger for about a half hour, the line that throws the exception is one inside of the vector.data()
method.
enabledLayerCount is the number of elements in the ppEnabledLayerNames array, not the length of the first string. The value you should be assigning is this->validationLayers.size()
.
Here archive are the spec valid usage requirements for ppEnabledLayerNames and enabledLayerCount.
If enabledLayerCount is not 0, ppEnabledLayerNames must be a valid pointer to an array of enabledLayerCount null-terminated UTF-8 strings
Additionally the documentation archive for the ppEnabledLayerNames struct member says
ppEnabledLayerNames is a pointer to an array of enabledLayerCount null-terminated UTF-8 strings containing the names of layers to enable for the created instance. The layers are loaded in the order they are listed in this array, with the first array element being the closest to the application, and the last array element being the closest to the driver. See the Layers archive section for further details.