LWJGL Forum

Programming => Vulkan => Topic started by: ilum on March 03, 2019, 08:07:56

Title: EXCEPTION_ACCESS_VIOLATION[SOLVED]
Post by: ilum on March 03, 2019, 08:07:56
Hi!
   I'm following https://vulkan-tutorial.com/Drawing_a_triangle/Setup/Instance (https://vulkan-tutorial.com/Drawing_a_triangle/Setup/Instance) and converting the code. I'm getting a access violation exception when the vkCreateInstance() function is executed.

Part of the code:

private void createInstance() {
        try(MemoryStack stack = MemoryStack.stackPush()) {
            VkApplicationInfo applicationInfo = VkApplicationInfo.mallocStack(stack);

            applicationInfo.sType(VK_STRUCTURE_TYPE_APPLICATION_INFO);
            applicationInfo.pApplicationName(memUTF8("Hello Triangle!"));
            applicationInfo.applicationVersion(VK_MAKE_VERSION(1, 0, 0));
            applicationInfo.pEngineName(memUTF8("No engine!"));
            applicationInfo.engineVersion(VK_MAKE_VERSION(1, 0, 0));
            applicationInfo.apiVersion(VK_API_VERSION_1_0);

            VkInstanceCreateInfo createInfo = VkInstanceCreateInfo.mallocStack(stack);

            createInfo.sType(VK_STRUCTURE_TYPE_INSTANCE_CREATE_INFO);
            createInfo.pApplicationInfo(applicationInfo);

            PointerBuffer glfwExtensions = glfwGetRequiredInstanceExtensions();
            createInfo.ppEnabledExtensionNames(glfwExtensions);

            PointerBuffer pInstance = stack.mallocPointer(1);

            int vkResult = vkCreateInstance(createInfo, null, pInstance);

            if (vkResult != VK_SUCCESS) {
                throw new AssertionError("Failed to create vk instance!");
            }

            vkInstance = new VkInstance(pInstance.get(0), createInfo);

            IntBuffer extensionCount = stack.mallocInt(1);
            vkEnumerateInstanceExtensionProperties((ByteBuffer) null, extensionCount, null);
            VkExtensionProperties.Buffer extensionProperties = VkExtensionProperties.mallocStack(extensionCount.get(), stack);
            vkEnumerateInstanceExtensionProperties((ByteBuffer) null, extensionCount, extensionProperties);

            extensionProperties.forEach(extensionProperty -> System.out.println(extensionProperty.extensionNameString()));
        }


I've also attached the error log. I can't really make sense of it but maybe someone could help. Thanks in advance !
Title: Re: EXCEPTION_ACCESS_VIOLATION
Post by: spasi on March 03, 2019, 12:48:27
The VkApplicationInfo & VkInstanceCreateInfo structs are allocated with mallocStack, but not all members are initialized. The Vulkan driver sees garbage values and crashes. Change mallocStack to callocStack and it should work.

You also have a bug in the vkEnumerateInstanceExtensionProperties calls, you should use extensionCount.get(0) when allocating the VkExtensionProperties buffer.
Title: Re: EXCEPTION_ACCESS_VIOLATION
Post by: ilum on March 03, 2019, 13:28:24
Quote from: spasi on March 03, 2019, 12:48:27
The VkApplicationInfo & VkInstanceCreateInfo structs are allocated with mallocStack, but not all members are initialized. The Vulkan driver sees garbage values and crashes. Change mallocStack to callocStack and it should work.

You also have a bug in the vkEnumerateInstanceExtensionProperties calls, you should use extensionCount.get(0) when allocating the VkExtensionProperties buffer.

Thanks a lot. That did the trick. Just to make sure I understand, because the structures have uninitialized members calloc is safer cause after memory allocation it initializes that block of memory to zero ? Nice eye for noticing the extensionCount issue :)
Title: Re: EXCEPTION_ACCESS_VIOLATION
Post by: spasi on March 03, 2019, 13:45:51
Quote from: parvu_eduard on March 03, 2019, 13:28:24calloc is safer cause after memory allocation it initializes that block of memory to zero ?

Correct. The malloc version is useful (i.e. faster) when you're going to write to every element of the buffer or struct before using it.