diff --git a/vma-rs/src/allocator.rs b/vma-rs/src/allocator.rs index 2162533..db306bb 100644 --- a/vma-rs/src/allocator.rs +++ b/vma-rs/src/allocator.rs @@ -115,6 +115,7 @@ impl AllocatorBuilder { if result == VK_SUCCESS { Ok(Allocator { allocator: unsafe { allocator.assume_init() }, + destroyed: false, }) } else { Err(anyhow::Error::msg(format!( @@ -143,14 +144,20 @@ impl Default for AllocatorBuilder { #[derive(Debug, Clone)] pub struct Allocator { allocator: VmaAllocator, + + destroyed: bool, } impl Allocator { pub fn allocate(&self) -> AllocationBuilder { + debug_assert!(!self.destroyed); + AllocationBuilder::new(self.allocator) } pub fn statistics(&self) -> VmaStats { + debug_assert!(!self.destroyed); + let mut stats = MaybeUninit::uninit(); unsafe { @@ -159,6 +166,16 @@ impl Allocator { stats.assume_init() } } + + pub fn destroy(&mut self) { + if !self.destroyed { + unsafe { + vmaDestroyAllocator(self.allocator); + } + + self.destroyed = true; + } + } } unsafe impl Send for Allocator {} @@ -172,8 +189,6 @@ impl Allocator { impl Drop for Allocator { fn drop(&mut self) { - unsafe { - vmaDestroyAllocator(self.allocator); - } + self.destroy() } } diff --git a/vulkan-rs/src/device.rs b/vulkan-rs/src/device.rs index cba83e8..e447f13 100644 --- a/vulkan-rs/src/device.rs +++ b/vulkan-rs/src/device.rs @@ -459,6 +459,7 @@ impl Drop for Device { .clear(self); } + self.memory_allocator.destroy(); self.destroy_device(); } }