From db525e660e4cfa53a9828769ba1548e33b769df6 Mon Sep 17 00:00:00 2001 From: Eero Lehtinen Date: Tue, 17 Sep 2024 01:54:01 +0300 Subject: [PATCH] Fix `MeshAllocator` panic (#14560) # Objective Fixes #14540 ## Solution - Clean slab layouts from stale `SlabId`s when freeing meshes - Technically performance requirements of freeing now increase based on the number of existing meshes, but maybe it doesn't matter too much in practice - This was the case before this PR too, but it's technically possible to free and allocate 2^32 times and overflow with `SlabId`s and cause incorrect behavior. It looks like new meshes would then override old ones. ## Testing - Tested in `loading_screen` example and tapping keyboard 1 and 2. --- crates/bevy_render/src/mesh/allocator.rs | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/crates/bevy_render/src/mesh/allocator.rs b/crates/bevy_render/src/mesh/allocator.rs index 218e19c475..94186517a4 100644 --- a/crates/bevy_render/src/mesh/allocator.rs +++ b/crates/bevy_render/src/mesh/allocator.rs @@ -588,6 +588,12 @@ impl MeshAllocator { } for empty_slab in empty_slabs { + self.slab_layouts.values_mut().for_each(|slab_ids| { + let idx = slab_ids.iter().position(|&slab_id| slab_id == empty_slab); + if let Some(idx) = idx { + slab_ids.remove(idx); + } + }); self.slabs.remove(&empty_slab); } }