diff --git a/Cargo.lock b/Cargo.lock index 02ce377567..9200b8ffc6 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -5637,6 +5637,7 @@ dependencies = [ "indexmap", "rand 0.8.3", "rayon", + "scopeguard", "serde", "slab", "specs", diff --git a/common/sys/Cargo.toml b/common/sys/Cargo.toml index 583ced842f..bbf88e2e73 100644 --- a/common/sys/Cargo.toml +++ b/common/sys/Cargo.toml @@ -35,6 +35,7 @@ serde = { version = "1.0.110", features = ["derive"] } tracy-client = { version = "0.10.0", optional = true } # Plugins +scopeguard = "1.1.0" toml = { version = "0.5.7", optional = true } tar = { version = "0.4.30", optional = true } wasmer = { version = "1.0.0", optional = true, default-features = false, features = ["wat", "default-cranelift", "default-jit"] } diff --git a/common/sys/src/plugin/memory_manager.rs b/common/sys/src/plugin/memory_manager.rs index a5a0f8f6d9..771e78b9e4 100644 --- a/common/sys/src/plugin/memory_manager.rs +++ b/common/sys/src/plugin/memory_manager.rs @@ -23,12 +23,14 @@ impl EcsAccessManager { // This function take a World reference and a function to execute ensuring the // pointer will never be corrupted during the execution of the function! pub fn execute_with(&self, world: &World, func: impl FnOnce() -> T) -> T { + let _guard = scopeguard::guard((), |_| { + // ensure the pointer is cleared in any case + self.ecs_pointer + .store(std::ptr::null_mut(), Ordering::Relaxed); + }); self.ecs_pointer .store(world as *const _ as *mut _, Ordering::Relaxed); - let out = func(); - self.ecs_pointer - .store(std::ptr::null_mut(), Ordering::Relaxed); - out + func() } /// This unsafe function returns a reference to the Ecs World