问题描述
我收到以下错误:
VkLayer_khronos_validation.dll 抛出异常在 0x00007FFA5252DD05 (VkLayer_khronos_validation.dll) 访问冲突读取位置 0xFFFFFFFFFFFFFFFF 处抛出异常.
我的代码:
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";
}
}
推荐答案
未初始化 VkDeviceQueueCreateInfo
.未初始化的指针 (pNext
) 会导致错误的指针取消引用.它发生在调试模式下,因为它使用调试内存模式.在 Release 模式下,一个未初始化的值通常是 0.
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.
在 Vulkan 的 C 绑定中处理此问题的典型方法之一是使用 {}
对 struct
进行零初始化.例如.VkDeviceQueueCreateInfo dqci = {};
.或者在 C99 和 C++20 中有指定的初始值设定项.对于大多数 Vulkan 参数,零是一个不错的默认值.
One of the typical ways to deal with this in C bindings of Vulkan is to zero-initialize struct
s 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.
这篇关于创建逻辑设备时获取访问冲突的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!