lua-users home
lua-l archive

[Date Prev][Date Next][Thread Prev][Thread Next] [Date Index] [Thread Index]


Hello!
 
I'm relatively new to Lua, and so have absolutely no idea as to what can be causing me to get this error. I haven't managed to find a small reproducible case, so I'll try and describe the problem as best I can.
 
I have a table (let's call it TableA) that has other tables as keys (with just true as the values). As the code gets run the equivalent of the following functions get called:
 
function RemoveTableFromTableA(table_to_remove)
    TableA[table_to_remove] = nil
end
 
function AddTableToTableA(table_to_add)
    TableA[table_to_add] = true
end
 
This goes on for a while, and then the following function will eventually throw the "Invalid key for 'next'" error:
 
function CountEntriesInTable(table_to_count)
    local count = 0
    for k in pairs(table_to_count) do
        count=count+1
    end
    return count
end
 
The loop that throws this error isn't altering the table it's iterating through, and although it's being run inside a coroutine all of the Lua code is being run on a single thread so nothing else can be accessing the table while the loop is iterating. I even put a breakpoint inside the garbage collector to see if that was firing off when the loop was running, but it wasn't. So my question is: why am I getting this error? Any hints or pointers gratefully received.
 
Thanks in advance,
 
    Jonathan
 
P.S. I'm using LuaPlus (which is based on Lua 5.0.2) but I'm not using its reference counted garbage collector features. The table in question isn't accessed from the C++ code, so I'm pretty sure that this is an issue I'm getting with Lua, rather than LuaPlus specifically.