Search code examples
memorygpuvulkanvram

Access Violation in Vulkan Memory Allocator


When attempting to allocate memory using VulkanMemoryAllocator, I receive an access violation on line 14781 of vk_mem_alloc.h. The error happens when the code reaches the vkGetBufferMemoryRequirements2KHR function, and when I attempt to add it outside of VulkanMemoryAllocator, I receive a linker error. I am using Vulkan Version 1.3, and from my understanding, I shouldn't need any extensions to use this function.

Here is my implementation of the code that is causing the access violation:

void VmaAllocator_T::GetBufferMemoryRequirements(
    VkBuffer hBuffer,
    VkMemoryRequirements& memReq,
    bool& requiresDedicatedAllocation,
    bool& prefersDedicatedAllocation) const
{
#if VMA_DEDICATED_ALLOCATION || VMA_VULKAN_VERSION >= 1001000
    if(m_UseKhrDedicatedAllocation || m_VulkanApiVersion >= VK_MAKE_VERSION(1, 1, 0))
    {
        VkBufferMemoryRequirementsInfo2KHR memReqInfo = { VK_STRUCTURE_TYPE_BUFFER_MEMORY_REQUIREMENTS_INFO_2_KHR };
        memReqInfo.buffer = hBuffer;

        VkMemoryDedicatedRequirementsKHR memDedicatedReq = { VK_STRUCTURE_TYPE_MEMORY_DEDICATED_REQUIREMENTS_KHR };

        VkMemoryRequirements2KHR memReq2 = { VK_STRUCTURE_TYPE_MEMORY_REQUIREMENTS_2_KHR };
        VmaPnextChainPushFront(&memReq2, &memDedicatedReq);
       
        (*m_VulkanFunctions.vkGetBufferMemoryRequirements2KHR)(m_hDevice, &memReqInfo, &memReq2);
        //Access violation here:
        memReq = memReq2.memoryRequirements;
        
        requiresDedicatedAllocation = (memDedicatedReq.requiresDedicatedAllocation != VK_FALSE);
        prefersDedicatedAllocation  = (memDedicatedReq.prefersDedicatedAllocation  != VK_FALSE);
    }
    else
#endif // #if VMA_DEDICATED_ALLOCATION || VMA_VULKAN_VERSION >= 1001000
    {
        (*m_VulkanFunctions.vkGetBufferMemoryRequirements)(m_hDevice, hBuffer, &memReq);
        requiresDedicatedAllocation = false;
        prefersDedicatedAllocation  = false;
    }

Solution

  • After multiple days of looking at my source and trying to figure out why this didn’t work, I finally figured it out. There was a problem in my VkApplicationInfo struct, which is used to create the Vulkan instance. In the engine version parameter of the struct, I put VK_VERSION_1_3. It should’ve been VK_API_VERSION_1_3.