I have run into the following conundrum trying to use Vulkan Hpp unique handles to store a buffer and its allocated memory. I declare the handles
vk::UniqueBuffer vertex_buffer;
vk::UniqueDeviceMemory vertex_buffer_memory;
and populate them using vk::Device::createBufferUnique
and vk::Device::allocateMemoryUnique
. These need to be called in this order since the latter depends on the memory requirements of the former (obtained via vk::Device::getBufferMemoryRequirements
). However, this results in vertex_memory_buffer
being destroyed before vertex_buffer
and triggers the following warning from validation layers:
Validation Warning: [ CHASSIS ] Object 0: handle = 0x558599c91fc0, type = VK_OBJECT_TYPE_DEVICE; Object 1: handle = 0x1b000000001b, type = VK_OBJECT_TYPE_BUFFER; Object 2: handle = 0x1c000000001c, type = VK_OBJECT_TYPE_DEVICE_MEMORY; | MessageID = 0x7366b7dd |
VK Object VkBuffer 0x1b000000001b[] still has a reference to mem obj VkDeviceMemory 0x1c000000001c[].
Indeed, all C API examples that I looked up destroy the buffer first and then free the memory. I can of course use some sort of workaround to ensure that the same happens here (e.g., default initializing vertex_buffer_memory
and assigning to it after creating vertex_buffer
), but that really defeats the purpose of unique handles (which is exactly to make the resource management automatic). Unless I'm misunderstanding something, there seems to be an incompatibility between the design of Vulkan API and RAII. What is the correct way of solving this issue?