Segmentation fault on vkCreateInstance

i’m new to vulkan and trying to migrate from using function prototypes to VK_NO_PROTOTYPES and getting function pointers myself, need to note that before i started to refactor this everything was fine. i’m loading function pointers like this

//global variables
PFN_vkGetInstanceProcAddr vkGetInstanceProcAddr;
PFN_vkCreateInstance vkCreateInstance;

// function that is called before creating instance
vkGetInstanceProcAddr = (PFN_vkGetInstanceProcAddr)SDL_Vulkan_GetVkGetInstanceProcAddr();
vkCreateInstance = (PFN_vkCreateInstance)vkGetInstanceProcAddr(nullptr, "vkCreateInstance");

i’m creating sdl window before calling function pointers loading, so getting vkGetInstanceProcAddr pointer should end up fine.
i add them in header

extern PFN_vkGetInstanceProcAddr vkGetInstanceProcAddr;
extern PFN_vkCreateInstance vkCreateInstance;

and then i’m trying to create an instance

VkInstance instance;

VkApplicationInfo appInfo = {};
appInfo.sType = VK_STRUCTURE_TYPE_APPLICATION_INFO;
appInfo.pApplicationName = "vulkan";
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_2;
appInfo.pNext = nullptr;

unsigned int extCount;
SDL_Vulkan_GetInstanceExtensions(window, &extCount, nullptr);
std::vector<const char*> extensions(extCount);
SDL_Vulkan_GetInstanceExtensions(window, &extCount, extensions.data());

VkInstanceCreateInfo instanceInfo = {};
instanceInfo.enabledLayerCount = 0;
instanceInfo.sType = VK_STRUCTURE_TYPE_INSTANCE_CREATE_INFO;
instanceInfo.pNext = nullptr;
instanceInfo.pApplicationInfo = &appInfo;
instanceInfo.enabledExtensionCount = extCount;
instanceInfo.ppEnabledExtensionNames = extensions.data();

if (vkCreateInstance(&instanceInfo, nullptr, &instance) != VK_SUCCESS)
		throw std::runtime_error("failed to create vulkan instance");

but when i run that code i receive segmentation fault, so i tried to debug it a little. i found that the error occurs at the vkCreateInstance call, but vkCreateInstance pointer is not NULL, std::cout << (vkCreateInstance == NULL) << std::endl outputs 0, also print vkCreateInstance in gdb outputs $1 = (PFN_vkCreateInstance) 0x555561bde8 <vkCreateInstance> so vkCreateInstance is difenetely not NULL. but what else can cause this to happen if it was working before moving to VK_NO_PROTOTYPES? when i revert loading function pointers back to prototypes, everything starts to work again.

For what purpose though?

Heh, why do people keep doing this? All of those members happily accept zero initialization…

SDL_Vulkan_GetInstanceExtensions reserves the right to fail, but return code is not checked.

Pre-null it to make clear whether it was overwritten.

I created an example with SDL2.

#include <SDL2/SDL.h>
#include <SDL2/SDL_vulkan.h>
#include <SDL_log.h>
#include <vector>
#include <vulkan/vulkan.h>
#include <vulkan/vulkan_core.h>

PFN_vkGetInstanceProcAddr vkGetInstanceProcAddr;
PFN_vkCreateInstance vkCreateInstance;

int main(int argc, char *argv[]) {
  if (SDL_Init(SDL_INIT_VIDEO) != 0) {
    SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "SDL_Init: %s\n",
                 SDL_GetError());
    return -1;
  }

  int state = SDL_Vulkan_LoadLibrary("C:\\Windows\\System32\\vulkan-1.dll");
  if (state == -1) {
    SDL_Log("SDL_Vulkan_LoadLibrary failed: %s", SDL_GetError());
    SDL_Quit();
    return -1;
  } else {
    SDL_Log("SDL_Vulkan_LoadLibrary success");
    vkGetInstanceProcAddr =
        (PFN_vkGetInstanceProcAddr)SDL_Vulkan_GetVkGetInstanceProcAddr();
    vkCreateInstance = (PFN_vkCreateInstance)vkGetInstanceProcAddr(
        nullptr, "vkCreateInstance");
    VkInstance instance;

    VkApplicationInfo appInfo = {};
    appInfo.sType = VK_STRUCTURE_TYPE_APPLICATION_INFO;
    appInfo.pApplicationName = "vulkan";
    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_2;
    appInfo.pNext = nullptr;

    unsigned int extCount{2};
    std::vector<const char *> extensions{"VK_KHR_surface", "VK_KHR_win32_surface"};

    VkInstanceCreateInfo instanceInfo = {};
    instanceInfo.enabledLayerCount = 0;
    instanceInfo.sType = VK_STRUCTURE_TYPE_INSTANCE_CREATE_INFO;
    instanceInfo.pNext = nullptr;
    instanceInfo.pApplicationInfo = &appInfo;
    instanceInfo.enabledExtensionCount = extCount;
    instanceInfo.ppEnabledExtensionNames = extensions.data();

    if (vkCreateInstance(&instanceInfo, nullptr, &instance) != VK_SUCCESS)
      throw std::runtime_error("failed to create vulkan instance");

    SDL_Log("Vulkan instance created.");
  }

  SDL_Vulkan_UnloadLibrary();
  SDL_Quit();

  return 0;
}

I had never used SDL2 before. So I can make some mistakes. But the Vulkan instance is successfully created. I also defined VK_NO_PROTOTYPES (in CMakeLists.txt).
About your code: I also use a shared library, and your code looks proper.