Skip to content

Commit

Permalink
Don't run destructors if Lua is gone
Browse files Browse the repository at this point in the history
  • Loading branch information
khvzak committed Jul 2, 2024
1 parent 617cf79 commit e3d138d
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 1 deletion.
6 changes: 6 additions & 0 deletions src/lua.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3377,10 +3377,16 @@ impl LuaInner {
}

impl WeakLua {
#[track_caller]
#[inline(always)]
pub(crate) fn lock(&self) -> LuaGuard {
LuaGuard::new(self.0.upgrade().unwrap())
}

#[inline(always)]
pub(crate) fn try_lock(&self) -> Option<LuaGuard> {
Some(LuaGuard::new(self.0.upgrade()?))
}
}

impl PartialEq for WeakLua {
Expand Down
4 changes: 3 additions & 1 deletion src/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -311,7 +311,9 @@ impl Clone for ValueRef {
impl Drop for ValueRef {
fn drop(&mut self) {
if self.drop {
self.lua.lock().drop_ref(self);
if let Some(lua) = self.lua.try_lock() {
lua.drop_ref(self);
}
}
}
}
Expand Down

0 comments on commit e3d138d

Please sign in to comment.