diff --git a/crates/bevy_ecs/src/storage/blob_vec.rs b/crates/bevy_ecs/src/storage/blob_vec.rs index 7469355cb78c6..d724728c177b2 100644 --- a/crates/bevy_ecs/src/storage/blob_vec.rs +++ b/crates/bevy_ecs/src/storage/blob_vec.rs @@ -199,13 +199,11 @@ impl BlobVec { impl Drop for BlobVec { fn drop(&mut self) { self.clear(); - if self.item_layout.size() > 0 { + let array_layout = + array_layout(&self.item_layout, self.capacity).expect("array layout should be valid"); + if array_layout.size() > 0 { unsafe { - std::alloc::dealloc( - self.get_ptr().as_ptr(), - array_layout(&self.item_layout, self.capacity) - .expect("array layout should be valid"), - ); + std::alloc::dealloc(self.get_ptr().as_ptr(), array_layout); std::alloc::dealloc(self.swap_scratch.as_ptr(), self.item_layout); } } @@ -388,4 +386,11 @@ mod tests { assert_eq!(*drop_counter.borrow(), 6); } + + #[test] + fn blob_vec_drop_empty_capacity() { + let item_layout = Layout::new::(); + let drop = TypeInfo::drop_ptr::; + let _ = BlobVec::new(item_layout, drop, 0); + } }