Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Memory leak #152

Open
guidanoli opened this issue Feb 10, 2021 · 2 comments
Open

Memory leak #152

guidanoli opened this issue Feb 10, 2021 · 2 comments

Comments

@guidanoli
Copy link
Contributor

guidanoli commented Feb 10, 2021

A dependency cycle avoids both Lua references and Python references from being collected by their respective garbage collection systems.

How to reproduce

This example uses __gc on tables, which is a feature not present in Lua 5.1, but is only there to show how the Lua object is not collected.

from lupa import LuaRuntime
lua = LuaRuntime()
lua_f = lua.eval('function(t) setmetatable(t, {__gc = print}) end')

###########################
# Normal behaviour
t = lua.table()
lua_f(t)
t = None
lua.execute('collectgarbage()')
# prints "table: <address>"
###########################

############################
# Unexpected behaviour
t = lua.table()
lua_f(t)
d = { "table": t }
t["dict"] = d
d = None
t = None
lua.execute('collectgarbage()')
# prints nothing --> table is not collected
#############################
@scoder
Copy link
Owner

scoder commented Apr 21, 2021

Is this impacted by the refcouting changes in #171?

@guidanoli
Copy link
Contributor Author

guidanoli commented Apr 21, 2021

Is this impacted by the refcouting changes in #171?

No, there is still a reference cycle.
Basically, in the example I gave, d is holding "hostage" the reference to _LuaTable, which holds "hostage" the reference to the Lua table in the registry (altough Python can't track the reference identifier down). In Lua, the table is holding "hostage" the reference to the userdata that points to d.

dict ---> _LuaTable (Python)
^                 |
|                 V
userdata <--- table (Lua)

I am using the word "hostage" here figuratively to emphasize that it's the only reference left!
The problem here is that while there is clearly for us a reference cycle, it is split between languages and they can't see the full picture.

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

No branches or pull requests

2 participants