mirror of
https://github.com/mlua-rs/mlua
synced 2026-06-08 16:05:43 +00:00
Fix userdata memleak in edge case.
This can happen if we unable to push metatable with `__gc` metamethod after pushing userdata. In this case Lua will never execute drop. Instead, we will push metatable first and then userdata.
This commit is contained in:
+4
-1
@@ -1977,8 +1977,11 @@ impl Lua {
|
||||
let _sg = StackGuard::new(self.state);
|
||||
check_stack(self.state, 2)?;
|
||||
|
||||
push_userdata(self.state, data)?;
|
||||
// If we unable to push metatable, then we should not push userdata.
|
||||
// Otherwise we can have a memory leak.
|
||||
self.push_userdata_metatable::<T>()?;
|
||||
push_userdata(self.state, data)?;
|
||||
ffi::lua_rotate(self.state, -2, 1);
|
||||
ffi::lua_setmetatable(self.state, -2);
|
||||
|
||||
Ok(AnyUserData(self.pop_ref()))
|
||||
|
||||
Reference in New Issue
Block a user