Skip to content

The memory leak of the luajit::function::remove #283

@yyy33

Description

@yyy33

I didn't call drop for the callback when I removed C. Can I change it this way

type Callback<'f> =
    Box<dyn Fn(*mut State) -> Result<c_int, crate::Error> + 'f>;

/// Removes the function reference stored in the Lua registry
pub fn remove(lua_ref: c_int) {
    unsafe {
        crate::with_state(|lstate| {
            ffi::lua_rawgeti(lstate, ffi::LUA_REGISTRYINDEX, lua_ref);
            ptr::drop_in_place(get_callback(lstate));
            ffi::lua_pop(lstate, 1);
            ffi::luaL_unref(lstate, ffi::LUA_REGISTRYINDEX, lua_ref)
        })
    }
}


unsafe fn get_callback(lstate: *mut State) -> *mut Callback<'static> {
    let idx = ffi::lua_upvalueindex(1);
    ffi::lua_touserdata(lstate, idx) as *mut Callback
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions