Lua, c++ and disappearing metatables -
Lua, c++ and disappearing metatables -
background
i work watusimoto on game bitfighter. utilize variation of luawrapper connect our c++ objects lua objects in game. utilize variation of lua called lua-vec speed vector operations.
we have been working solve bug time has eluded us. random crashes occur suggest corrupt metatables. see here watusimoto's post on issue. i'm not sure because of corrupt metatable , have seen odd behavior wish inquire here.
the problem manifestation
as example, create object , add together level this:
t = textitem.new() t:settext("hello") levelgen:additem(t)
however, game (not always) crash. error:
attempt phone call missing or unknown method 'additem' (a nil value)
using suggestion given in reply watusimoto's post mentioned above, have changed lastly line following:
local ok, res = pcall(function() levelgen:additem(t) end) if not ok local s = "invalid levelgen value: "..tostring(levelgen).." "..type(levelgen).."\n" k, v in pairs(getmetatable(levelgen)) s = s.."meta "..tostring(k).." "..tostring(v).."\n" end error(res..s) end
this prints out metatable levelgen
if when wrong calling method it.
however, , crazy, when fails , prints out metatable, metatable exactly how should (with right additem
phone call , everything). if print metatable levelgen
upon script load, , when fails using pcall
above, identical, every phone call , pointer userdata same , should be.
it though metatable levelgen
spontaneously disappearing @ random.
would have thought going on?
thank you
note: doesn't happen levelgen
object. instance, has happened on testitem
object mentioned above well. in fact, same code crashes on computer @ line levelgen:additem(t)
crashes on developer's computer line t:settext("hello")
same error message missing or unknown method 'settext' (a nil value)
as mystery, need peel off layer layer. recommend going through same steps lua going , trying observe path taken diverge expectations:
what getmetatable(levelgen).__index
return? if it's table, check content additem
. if it's function, seek phone call (table, "additem")
, see returns.
check if getmetatable
returns reference same object before , after phone call (or when fails).
are there several levels of metatable indirection phone call going through? if so, seek follow same path explicit calls , see differences are.
are using weak
keys may cause values disappear if there no other references?
can provide "default" value when observe fails , go on see if "finds" method 1 time again later? or when it's broken, it's broken every phone call after that?
what if save proper value additem , "fix" when observe it's broken?
what if handle error (as do) , phone call 10 times? show valid results @ to the lowest degree 1 time (after fails)? 100 times? if maintain calling same method when works, fail? may help come more reproducible error.
i'm not familiar luawrapper provide more specific questions, these steps i'd take if you.
c++ lua metatable
Comments
Post a Comment