Potential resource leak was fixed in LuaHandle - #798
Open
alex-aparin wants to merge 1 commit into
Open
Conversation
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. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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:
if this == &rhsis just check for code likeluahandle 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