lua-users home
lua-l archive

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


> A quick question: if I lock an object with
> ref=lua_ref(L, 1), and then unlock it with lua_ref(L,
> ref), is 'ref' still a valid Lua object? Meaning, if I
> unlock an object from C++, will the reference stay
> valid as long as Lua continues to hold the object, or
> do I need to create a new, unlocked reference?

I am not sure of what you mean, but let me try to clarify the Lua
reference mechanism so that you can draw your conclusions.

When you do ref=lua_ref(L, 1), Lua reserves space in a reference vector
for that object, saves the object in that vector and returns an index to
you. When you call lua_getref(L, ref), Lua pushes the corresponding
index from that vector in the Lua stack. When you call lua_unref(L,
ref), Lua marks the position on the vector as free for further lua_ref
calls so that the value of ref is meaningless after the call to

Also, if you call lua_ref(L, 1) (and not lua_ref(L, 0)), Lua locks the
object by marking in the reference vector that this object should not be
garbage collected. The garbage collector checks this vector before the
collection process so that your locked objects are safe. On the other
hand, a call lua_ref(L, 0) does not guarantee your object will not be

Did that answer your question?


PS: Lua gurus, please correct me if I am wrong.