简体   繁体   中英

Creating Vulkan instance causes access violation

I am trying to initialize the Vulkan API.
The problem I am having is that I get an access violation error after I call vkCreateInstance and I think the problem comes from the extension and layer lists.
I am using a char buff[20][256] to transfer them from strings to the structure for the API call, and the layer and extension names I see in the debugger(3 extensions and 15 layers) are all a lot shorter than 256 characters and are all null terminated.
There is no buffer overflow with the extension or layer names, yet it crashes.

The layer and extension lists of strings I received trough using vkEnumerateInstanceExtensionProperties and vkEnumerateInstanceLayerProperties beforehand and are all valid null-terminated strings like "VK_KHR_surface", etc.

Is it possible that even tho it says I support some extensions, that I don't really support them and the API is crashing when it's trying to initialize an extension I don't support?

            void InitializeInstance(void** instance, const vector<string>& layers, const vector<string>& extensions)
    {
        VkApplicationInfo applicationInfo;
            VkInstanceCreateInfo instanceInfo;
            VkInstance* instanceOut = (VkInstance*)instance;

            applicationInfo.sType = VK_STRUCTURE_TYPE_APPLICATION_INFO;
            applicationInfo.pNext = nullptr;
            applicationInfo.pApplicationName = "MyApp";
            applicationInfo.pEngineName = "MyEngine";
            applicationInfo.engineVersion = 1;
            applicationInfo.apiVersion = VK_API_VERSION_1_0;

            instanceInfo.sType = VK_STRUCTURE_TYPE_INSTANCE_CREATE_INFO;
            instanceInfo.pNext = null;
            instanceInfo.flags = 0;
            instanceInfo.pApplicationInfo = &applicationInfo;

            char buffLayer[20][256];
            char buffExt[20][256];

            if(!layers.empty())
            {
                instanceInfo.enabledLayerCount = layers.size();

                for(int i = 0; i < layers.size(); i++)
                {
                    strcpy(buffLayer[i], layers[i].c_str());
                }
                instanceInfo.ppEnabledLayerNames = (char**)buffLayer;
            }
            else
            {
                instanceInfo.enabledLayerCount = 0;
                instanceInfo.ppEnabledLayerNames = nullptr;
            }

            if(!extensions.empty())
            {
                instanceInfo.enabledExtensionCount = extensions.size();

                for(int i = 0; i < extensions.size(); i++)
                {
                    strcpy(buffExt[i], extensions[i].c_str());
                }
                instanceInfo.ppEnabledExtensionNames = (char**)buffExt;
            }
            else
            {
                instanceInfo.enabledExtensionCount = 0;
                instanceInfo.ppEnabledExtensionNames = nullptr;
            }

vkCreateInstance(&instanceInfo, nullptr, instanceOut);
        }

When I have only 0 extensions AND 0 layers, it creates successfully. If any of them is not 0, it crashes.

char buffLayer[20][256];
instanceInfo.ppEnabledLayerNames = (char**)buffLayer;

ppEnabledLayerNames is supposed to be an array of pointers to character arrays. But you're passing it a 2D array of characters, which is effectively just an array of 20*256 characters.

If you're on a machine with 32-bit pointers, the driver is going to take the first four bytes in buffLayer and treat them as a pointer to a character array. But you've just stored the first four characters of a layer name there, and 'VK_K' is probably not going to be a valid pointer value :). So the loader will crash when trying to dereference that invalid pointer.

Probably the simplest change would be to add:

char* layerNames[20];
for (int i = 0; i < 20; i++)
    layerNames[i] = &buffLayer[i][0];

and pass layerNames as ppEnabledLayerNames.

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