lua-users home
lua-l archive

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


Is it safe to use string values retrieved from a table even though the value is popped off the stack?  For example:

Lua side:

table = { name="test1234" }

C side:

/* assume "table" is passed as argument to this function */
int SomeFunction(lua_State* L)
{
   lua_getfield(L, 1, "name");
   const char* name = lua_tostring(L, -1);
   lua_pop(L, 1);

   ... use "name" char* variable...
}

I know this is not always safe because the garbage collector might take the string after it is popped off this stack but in this case the string still exists in the table and will not be collected until "table" is collected (table is still on the stack).  So is this example safe or is the string from lua_getfield actually a different string than the one in the table?

--
// Chris