Skip to content

Potential resource leak was fixed in LuaHandle - #798

Open
alex-aparin wants to merge 1 commit into
FOME-Tech:masterfrom
alex-aparin:fix/lua_handle
Open

Potential resource leak was fixed in LuaHandle#798
alex-aparin wants to merge 1 commit into
FOME-Tech:masterfrom
alex-aparin:fix/lua_handle

Conversation

@alex-aparin

@alex-aparin alex-aparin commented Aug 29, 2026

Copy link
Copy Markdown

Move assingment loses lua_State pointer + explicit constructor to avoid extra captures. More detailed comments here rusefi/rusefi#10154

UPD: attached comments from link:

This PR is related with small improvements for LuaHandle:

Usually it's good idea to have explicit constructor in order to prevent any accidential captures of native handles.
Move assingment had potential memory leak. Currently this operator is not used in codebase, but I decided to fix it just in case.
P.S. These fixes lead me to the thought to create special allocator to check potential mem leaks in unit tests. Is it good idea to cover such cases? I may add it later in separate PR

Actually potential memory leak is in original move assignment constructor. If we have code like this

LuaHandle luastate1{lua_newstate(...)};
// after this lua_close for luastate1's orinigal native handle will not called
// it contains new state, moved object contains null
luastate1 = LuaHandle{lua_newstate(.. .)};

My fix is just to swap handles without overwite, destructor of moved object correctly close handle. Other changes are not related with mem leak.

Explicit constructor is just syntax hint to avoid pitfalls when you can accidentally capture and destroy handle. For example:

void foo(LuaHandle);
void bar(lua_State*);
lua_State* a = ...
foo(a);
bar(a); // error! a handle is released

if this == &rhs is just check for code like luahandle a; a = std::move(a) it can be freely omitted at all, because at the moment lua handle does not contain any complex logic and this example looks synthetic and buggy, check is not required. If you have additional questions or examples feel free to ask

@mck1117

mck1117 commented Aug 29, 2026

Copy link
Copy Markdown
Collaborator

I see lots of explanation on the linked issue, that explanation belongs in the PR description because that's the most durable place for it.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants