Lua, C ++ and Disappearing Meta Tags

Background

I work with Watusimoto in a Bitfighter game . We use a variation of LuaWrapper to connect our C ++ objects to Lua objects in the game. We also use a Lua variation called lua-vec to speed up vector operations.

We have been working on a solution to a problem for some time that has eluded us. Random crashes occur that suggest metadata corruption. See here for Watusimoto's post on this. I'm not sure if this is because of the corrupt meta tag and I saw some really strange behavior that I want to ask about here.

Problem manifestation

As an example, we create an object and add it to this level:

t = TextItem.new()
t:setText("hello")
levelgen:addItem(t)

However, the game sometimes (not always) crashes. With an error:

attempt to call missing or unknown method 'addItem' (a nil value)

Using the sentence given in response to the Watusimoto message mentioned above, I changed the last line to the following:

local ok, res = pcall(function() levelgen:addItem(t) end)

if not ok then
    local s = "Invalid levelgen value: "..tostring(levelgen).." "..type(levelgen).."\n"

    for k, v in pairs(getmetatable(levelgen)) do 
        s = s.."meta "..tostring(k).." "..tostring(v).."\n"
    end

    error(res..s)
end

This returns a metathe for levelgenif something happens if the method was called incorrectly from it.

However, and this is crazy, when it fails and prints the meta meta, the meta meta should be exactly the same (with the right challenge addItemand all). If I print the metathe to load levelgenwhen the script loads, and if the pcallabove is unsuccessful , they are identical, each call and pointer to userdata will be the same as it should be.

As if the metathete for levelgenspontaneously disappears randomly.

Can anyone understand what is going on?

thank

: levelgen. , TestItem, . levelgen:addItem(t), t:setText("hello") missing or unknown method 'setText' (a nil value)

+5
3

(commit 3c54015) LuaWrapper . , LuaWrapper.

!

+1

, . , Lua, , :

getmetatable(levelgen).__index? , addItem. , (table, "addItem") , .

, getmetatable ( ).

, ? , , .

weak , , ?

" ", , , , "" ? , ?

, addItem "" , ?

, ( ) 10 ? ( )? 100 ? , , ? .

LuaWrapper, , , , .

+2

, , , :

struct Foo
{
    Bar bar;
    // Other fields follow
}

Foo Bar Lua LuaWrapper. , bar - Foo. , , - , , LuaWrapper.

LuaWrapper , , (, , Lua). . , , , Foo Bar , , , LuaWrapper .

This can lead to the capture of the wrong object, meta-meta when trying to find a method. Clearly, since he is looking at the wrong metatable, he will not find the method that you need, and therefore it will look as if your metatable has mysteriously lost records.

I checked the changes that track every data object in each type, and not in one giant heap. If you upgrade a copy of LuaWrapper to the latest from the repository, I am sure your problem will be fixed.

+2
source

All Articles