From d1b59e008f0bd6de407ef6f2c9a575fb5b5506b3 Mon Sep 17 00:00:00 2001 From: hodasemi Date: Thu, 4 Apr 2024 14:58:54 +0200 Subject: [PATCH] Make future not hold strong handles --- src/states.rs | 13 +++++++------ 1 file changed, 7 insertions(+), 6 deletions(-) diff --git a/src/states.rs b/src/states.rs index f28e978..9ecbb1c 100644 --- a/src/states.rs +++ b/src/states.rs @@ -317,10 +317,11 @@ impl States { &self, id: impl Into>, ) -> Result> { - let state = id.into().map(|id| self.get_state(id)).transpose()?; - let current_state = self.current_state.clone(); + let state: Option> = id.into().map(|id| self.get_state(id)).transpose()?; + let weak_state = state.map(|s| Arc::downgrade(&s)); + let weak_current_state = Arc::downgrade(&self.current_state); let gui_handler = if self.control_top_gui { - Some(self.gui_handler.clone()) + Some(Arc::downgrade(&self.gui_handler)) } else { None }; @@ -328,9 +329,9 @@ impl States { Ok(Box::new(move || { Self::_set_state( - state.clone(), - &mut *current_state.lock().unwrap(), - gui_handler.clone(), + weak_state.as_ref().map(|w| w.upgrade()).flatten(), + &mut *weak_current_state.upgrade().unwrap().lock().unwrap(), + gui_handler.as_ref().map(|h| h.upgrade()).flatten(), logging, ) }))