Home > database >  Getting a read access violation trying to spin up a Vulkan instance
Getting a read access violation trying to spin up a Vulkan instance

Time:10-24

I am trying to use the vkCreateInstance() method but I am getting a read access violation from vulkan-1.dll. I am using SDL for windowing, and am using Visual Studio 2022, and have both Windows and my graphics drivers up to date

#include <vulkan/vulkan.hpp>

#include <SDL.h>
#include <SDL_vulkan.h>
#include <vector>
#include <iostream>

SDL_Window* window;
int main(int argc, char *argv[]) {
    if (SDL_Init(SDL_INIT_VIDEO) < 0) {
        std::cout << "Video initialization failed:" << std::endl;
    }
    window = SDL_CreateWindow("vkTest", SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, SCREEN_WIDTH, SCREEN_HEIGHT, SDL_WINDOW_SHOWN | SDL_WINDOW_VULKAN);
    SDL_vulkanInstance inst;
    VkInstanceCreateInfo inst_info;
    VkApplicationInfo appInfo;
    appInfo.sType = VK_STRUCTURE_TYPE_APPLICATION_INFO;
    appInfo.pApplicationName = "engine";
    appInfo.applicationVersion = VK_MAKE_VERSION(1, 0, 0);
    appInfo.pEngineName = "engine";
    appInfo.engineVersion = VK_MAKE_VERSION(1, 0, 0);
    appInfo.apiVersion = VK_API_VERSION_1_0;

    inst_info.sType = VK_STRUCTURE_TYPE_INSTANCE_CREATE_INFO;
    inst_info.pApplicationInfo = &appInfo;

    inst_info.flags = 0;

    uint32_t vkExtensionCount = 0;
    
    if (!SDL_Vulkan_GetInstanceExtensions(window, &vkExtensionCount, NULL)) {
        std::cout << "Unable to get Vulkan extensions." << std::endl;
        exit(EXIT_FAILURE);
    }

    std::vector<const char*> vkExtensions;
    vkExtensions.resize(vkExtensionCount);

    

    if (!SDL_Vulkan_GetInstanceExtensions(window, &vkExtensionCount, vkExtensions.data())) {
        std::cout << "Unable to get Vulkan extensions." << std::endl;
        exit(EXIT_FAILURE);
    }
    std::cout << *vkExtensions.data() << std::endl;

    inst_info.enabledExtensionCount = vkExtensionCount;
    inst_info.ppEnabledExtensionNames = vkExtensions.data();

    inst_info.enabledLayerCount = 0;

    VkResult res = vkCreateInstance(&inst_info, nullptr, &inst);
    if (res == VK_ERROR_INCOMPATIBLE_DRIVER) {
        std::cout << "Cannot find compadible vulkan driver." << std::endl;
        exit(EXIT_FAILURE);
    }
    else if (res) {
        std::cout << "unknown error" << std::endl;
        exit(EXIT_FAILURE);
    }
}

CodePudding user response:

The most likely cause for this is that your VkInstanceCreateInfo does contain uninitialized values. You declare it as VkInstanceCreateInfo inst_info; without initializing the structure. Later on you only set some members of it to defined values, thus passing uninitialized values to the implementation, which then fails at interpreting them. VkInstanceCreateInfo e.g. has a pNext member, which is a pointer chain to structures that can contain additional parameters for the instance creation. If this is uninitialized the implementation tries to interpret this as a pointer which will result in a read error.

The correct way would be either setting all members of inst_info of zero-initializing it like this:

VkInstanceCreateInfo inst_info{};
inst_info.sType = VK_STRUCTURE_TYPE_INSTANCE_CREATE_INFO;
inst_info.pApplicationInfo = &appInfo;

In general (not only for Vulkan) you should zero-initialize all structs.

  • Related