0

I get the following error:

Exception thrown at VkLayer_khronos_validation.dll Exception thrown at 0x00007FFA5252DD05 (VkLayer_khronos_validation.dll) Access violation reading location 0xFFFFFFFFFFFFFFFF.

My code:

void LogicalDevice::createLogicalDevice(VkPhysicalDevice pDevice){
VkDeviceQueueCreateInfo qcreateInfo;
qcreateInfo.sType = VK_STRUCTURE_TYPE_DEVICE_QUEUE_CREATE_INFO;
QueueFamiliesIndices indices = PhysicalDevice::findQueueFamilies(pDevice);
qcreateInfo.queueFamilyIndex = indices.graphicsFamily.value();
qcreateInfo.queueCount = 1;
float queuePriority = 1.0f;
qcreateInfo.pQueuePriorities = &queuePriority;

VkPhysicalDeviceFeatures deviceFeatures = {};

VkDeviceCreateInfo createInfo = {};
createInfo.sType = VK_STRUCTURE_TYPE_DEVICE_CREATE_INFO;
createInfo.pQueueCreateInfos = &qcreateInfo;
createInfo.queueCreateInfoCount = 1;
createInfo.pEnabledFeatures = &deviceFeatures;

createInfo.enabledExtensionCount = 0;

if (ValidationLayers::enableValidationLayers){
     createInfo.enabledLayerCount = static_cast<uint32_t>(ValidationLayers::validationLayers.size());
     createInfo.ppEnabledLayerNames = ValidationLayers::validationLayers.data();

}
else{
     createInfo.enabledLayerCount = 0;
     createInfo.pNext = nullptr;
}
createInfo.flags = 0;

if (vkCreateDevice(pDevice, &createInfo ,nullptr, &device) != VK_SUCCESS){
    std::cout << "Failed to create Logical Device";
}

}

1 Answers1

1

Uninitialized VkDeviceQueueCreateInfo. Unitialized pointer (pNext) leads to bad pointer dereference. It happens in the Debug mode, because it uses debug memory pattern. In Release mode an uninitialized value often happens to be 0.

One of the typical ways to deal with this in C bindings of Vulkan is to zero-initialize structs with {}. E.g. VkDeviceQueueCreateInfo dqci = {};. Or there are designated initializers in C99 and C++20. Zero is a decent default value for majority of Vulkan parameters.

krOoze
  • 12,301
  • 1
  • 20
  • 34
  • I think you should clarify how {} initializes things, since it is possible the above user does not see that as an important step. – Krupip Feb 13 '20 at 14:53
  • Sure, why not. It is not even a Vulkan-specific issue though, and I hope it is a typo. Anyone dealing with Vulkan should already have a decent handle on C/C++ or programming generally... – krOoze Feb 13 '20 at 16:02
  • zero initialization via {}, even if taught is really only used for c structs like in vulkan with no proper constructors. You can program in c++ your whole life with out encountering that initialization, vulkan was the first time in 5 years I had encountered that construct, it is absolutely not typical. – Krupip Feb 13 '20 at 16:12
  • Well, better late than never :p. As known, initialization in C++ is bonkers. Though uninitialized values is basic knowledge. The `{}` fix is but one way, and basically an opinion based suggestion from us. – krOoze Feb 13 '20 at 16:43