diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 56e7b468e8..abb68fee29 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -99,11 +99,10 @@ jobs: RUSTFLAGS: -Zrandomize-layout # https://github.com/rust-lang/miri#miri--z-flags-and-environment-variables # -Zmiri-disable-isolation is needed because our executor uses `fastrand` which accesses system time. - # -Zmiri-ignore-leaks is needed because running bevy_ecs tests finds a memory leak but its impossible - # to track down because allocids are nondeterministic. # -Zmiri-permissive-provenance disables warnings against int2ptr casts (since those are used by once_cell) # -Zmiri-disable-weak-memory-emulation works around https://github.com/bevyengine/bevy/issues/5164. - MIRIFLAGS: -Zmiri-disable-isolation -Zmiri-ignore-leaks -Zmiri-permissive-provenance -Zmiri-disable-weak-memory-emulation + # -Zmiri-ignore-leaks is necessary because a bunch of tests don't join all threads before finishing. + MIRIFLAGS: -Zmiri-ignore-leaks -Zmiri-disable-isolation -Zmiri-permissive-provenance -Zmiri-disable-weak-memory-emulation check-compiles: runs-on: ubuntu-latest diff --git a/crates/bevy_ecs/src/storage/blob_vec.rs b/crates/bevy_ecs/src/storage/blob_vec.rs index 14d584b048..1a4404d9b2 100644 --- a/crates/bevy_ecs/src/storage/blob_vec.rs +++ b/crates/bevy_ecs/src/storage/blob_vec.rs @@ -151,8 +151,20 @@ impl BlobVec { let ptr = self.get_unchecked_mut(index).promote().as_ptr(); self.len = 0; // Drop the old value, then write back, justifying the promotion + // If the drop impl for the old value panics then we run the drop impl for `value` too. if let Some(drop) = self.drop { + struct OnDrop(F); + impl Drop for OnDrop { + fn drop(&mut self) { + (self.0)(); + } + } + let value = value.as_ptr(); + let on_unwind = OnDrop(|| (drop)(OwningPtr::new(NonNull::new_unchecked(value)))); + (drop)(OwningPtr::new(NonNull::new_unchecked(ptr))); + + core::mem::forget(on_unwind); } std::ptr::copy_nonoverlapping::(value.as_ptr(), ptr, self.item_layout.size()); self.len = old_len; @@ -304,12 +316,16 @@ impl BlobVec { impl Drop for BlobVec { fn drop(&mut self) { self.clear(); + if self.item_layout.size() > 0 { + unsafe { + std::alloc::dealloc(self.swap_scratch.as_ptr(), self.item_layout); + } + } 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_mut().as_ptr(), array_layout); - std::alloc::dealloc(self.swap_scratch.as_ptr(), self.item_layout); } } } diff --git a/crates/bevy_ecs/src/world/mod.rs b/crates/bevy_ecs/src/world/mod.rs index 5f4a952e62..d220495a82 100644 --- a/crates/bevy_ecs/src/world/mod.rs +++ b/crates/bevy_ecs/src/world/mod.rs @@ -1693,6 +1693,7 @@ mod tests { DropLogItem::Create(0), DropLogItem::Create(1), DropLogItem::Drop(0), + DropLogItem::Drop(1), ] ); }