简体   繁体   中英

Error creating Vulkan instance when using layers

I'm learning to use vulkan, when I don't use layers passing the parameter as nullptr the instance is created without errors, if not, the following error occurs at vkCreateInstance :

(vulkan-1.dll) Access violation reading location 0xFFFFFFFFFFFFFFFF .

There is my code:

bool IEVkRenderer::CreateInstance()
    {
        ...
        std::vector<const char*> extensions;
        extensions.push_back("VK_KHR_surface");

        if (enableValidationLayers)
            extensions.push_back("VK_EXT_debug_report");

        extensions.push_back("VK_KHR_win32_surface");

        VkInstanceCreateInfo createInfo;
        createInfo.sType = VK_STRUCTURE_TYPE_INSTANCE_CREATE_INFO;
        createInfo.pNext = nullptr;
        createInfo.pApplicationInfo = &appInfo;
        createInfo.enabledExtensionCount = static_cast<uint32_t>(extensions.size());
        createInfo.ppEnabledExtensionNames = extensions.data();

        if (enableValidationLayers) {

            std::vector<const char*> layers;
            layers.push_back("VK_LAYER_LUNARG_standard_validation");

            if (!CheckValidationLayersSupport(layers)) {
                lError("VK Renderer", "Validation layer not supported");
                return false;
            }

            createInfo.enabledLayerCount = static_cast<uint32_t>(layers.size());
            createInfo.ppEnabledLayerNames = layers.data();

        }else {

            createInfo.enabledLayerCount = 0;
            createInfo.ppEnabledLayerNames = nullptr;

        }

        if (vkCreateInstance(&createInfo, nullptr, &m_Instance) != VK_SUCCESS)
            return false;

        return true;
    }

You are using memory past delete :

if (enableValidationLayers) {
    std::vector<const char*> layers;
    createInfo.ppEnabledLayerNames = layers.data();
}
vkCreateInstance(&createInfo, nullptr, &m_Instance)

Furthermore, your createInfo.flags is uninitialized value, which is invalid usage. Per spec it must be 0 . The established practice when using C bindings of Vulkan in C++ is to preinitialize the struct like so:

VkInstanceCreateInfo createInfo = {};

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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