Home > Enterprise >  vkCreateInsatance segmentation fault
vkCreateInsatance segmentation fault

Time:08-03

I followed by vulkan tutorial, and i get segfault on vkCreateInstance.

I use arch (btw), and i install vulkan by paru -S vulkan-devel (also linux-firmware and mesa are installed). My CPU is AMD Ryzen 7 2700, and GPU is Radeon RX 580 4GB.

I saw a similar question, but the answer did not solve my problem (and besides, they use C , not C)

#include <stdio.h>
#include <stdlib.h>

#include <vulkan/vulkan.h>
#define GLFW_INCLUDE_VULKAN
#include <GLFW/glfw3.h>

const char* APPLICATION_NAME = "Vulkan Teach";
const uint16_t WIDTH = 800;
const uint16_t HEIGHT = 600;

static GLFWwindow* window;
static VkInstance instance;

void error(char* message, int code) {
    fprintf(stderr, "%s (status code: %d)\n", message, code);
    exit(code);
}

void initWindow() {
    glfwInit();

    glfwWindowHint(GLFW_CLIENT_API, GLFW_NO_API);
    glfwWindowHint(GLFW_RESIZABLE, GLFW_FALSE);

    window = glfwCreateWindow(WIDTH, HEIGHT, APPLICATION_NAME, NULL, NULL);
}

void mainLoop() {
    while(!glfwWindowShouldClose(window)) {
        glfwPollEvents();
    }
}

void createInsance() {
    VkApplicationInfo appInfo;
    appInfo.sType = VK_STRUCTURE_TYPE_APPLICATION_INFO;
    appInfo.pApplicationName = APPLICATION_NAME;
    appInfo.applicationVersion = VK_MAKE_VERSION(1, 0, 0);
    appInfo.pEngineName = "No Engine";
    appInfo.engineVersion = VK_MAKE_VERSION(1, 0, 0);
    appInfo.apiVersion = VK_API_VERSION_1_0;

    VkInstanceCreateInfo createInfo;
    createInfo.sType = VK_STRUCTURE_TYPE_INSTANCE_CREATE_INFO;
    createInfo.pApplicationInfo = &appInfo;

    uint32_t glfwExtensionsCount = 0;
    const char** glfwExtensions;
    glfwExtensions = glfwGetRequiredInstanceExtensions(&glfwExtensionsCount);

    createInfo.enabledExtensionCount = glfwExtensionsCount;
    createInfo.ppEnabledExtensionNames = glfwExtensions;

    createInfo.enabledLayerCount = 0;

    VkResult result = vkCreateInstance(&createInfo, NULL, &instance);
    if(result != VK_SUCCESS) {
        error("Failed to create vulkan instance.", result);
    }
}

void initVulkan() {
    createInsance();
}

void cleanup() {
    vkDestroyInstance(instance, NULL);

    glfwDestroyWindow(window);
    glfwTerminate();
}

int main() {
    initWindow();
    initVulkan();
    mainLoop();

    cleanup();
    return 0;
}

CodePudding user response:

You are most probably passing uninitialized values to your instance creation. This part of the code:

VkInstanceCreateInfo createInfo;
createInfo.sType = VK_STRUCTURE_TYPE_INSTANCE_CREATE_INFO;
createInfo.pApplicationInfo = &appInfo;

Only initializes the sType and pApplicationInfo members of the create info, while other important members such as pNext are uninitialized and as such may contain values that the driver doesn't know how to handle, resulting in the segfault.

So as a general rule for all (Vulkan) structs: Zero initialize like this:

VkInstanceCreateInfo createInfo = {0};
  • Related