ThePhD / sol2

Sol3 (sol2 v3.0) - a C++ <-> Lua API wrapper with advanced features and top notch performance - is here, and it's great! Documentation:
http://sol2.rtfd.io/
MIT License
4.12k stars 500 forks source link

Custom containers holding Lua tables #1443

Open uramer opened 1 year ago

uramer commented 1 year ago

Hello.

What's the recommended way to create custom containers, which can hold indirect self-references? From my testing, they never get garbage-collected.

Example Lua code

do
  local container = myApi.makeContainer()
  container:insert({
    callback = function()
      container:insert({}) -- capture container in a closure
    end
  })
end
collectGarbage()
-- container doesn't get garbage-collected because of the cyclic reference

The cases I tested were basically various std::vector / std::map wrappers, or even just a struct with a sol::table field, passed to Lua as userdata with usertypes.

Is such a pattern not supported at all, and all such containers should be implemented on the Lua side?