-
Notifications
You must be signed in to change notification settings - Fork 88
Description
Browsing the sources and testing around got me thinking that there's no trivial solution to a cyclic referencing problem on Lua side, admittedly I'm no expert in the field.
Currently if I do something like that
local py = require("python")
local weak_refs = setmetatable({}, { __mode = "v" })
do
local pb = py.builtins()
local control_table = {}
-- reference itself in a table - control_table = {{control_table,},}
table.insert(control_table, {control_table})
weak_refs["control_table"] = control_table
local mixed_table = {}
list = pb.list()
list.append(mixed_table)
-- reference itself in a table - mixed_table = {py_list(mixed_table),}
table.insert(mixed_table, list)
weak_refs["mixed_table"] = mixed_table
end
collectgarbage('collect')
for k, v in pairs(weak_refs) do
print(k, "is not collected! with value of:", v)
endOutput
mixed_table is not collected! with value of: table: 0x226797e0
Since I don't see a way to let the garbage collector to traverse a userdata I don't see a solution to this currently.
Untested but the issue would exist on the Python side too but it can be solved with: https://docs.python.org/3/c-api/gcsupport.html
You would have to build a traverser that would traverse Lua tables and other Python objects, but you would have to convert Lua tables into Python objects too and those have to be cached to avoid revisiting existing nodes, caching could be done with weak referenced table in lua, the key would be the cached lua object and the value is userdata with the cached PyObject.
Is there something obvious that I missed? Is there a way of solving this on the Lua side ?