lua-users home
lua-l archive

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


Hi

I have a list of userdatas in a weak table. If I create an object in C, I store
that userdata in that table. The objects are created in C without knowing the
luastate and thus, I cannot store the references directly on the stack which is
used for returning the objects to the luascript. 
Because of this, sometimes a reference is garbagecollected just after its
creation and before it was returned to the luascript.
I tried out to stop the GC during this operation, but restarting the GC seems to
be somewhat expensive. I see now two different ways to approach this:

1) storing references in a non-weak table during critical operations
2) setting the metatable of the weaktable to nil during critical operations
(making it non-weak)

I know that it is not safe to change the __mode value of a metatable, however, I
wonder if it is ok to unset the metatable - or is that unsafe too? (the
documentation here says

"After you use a table as a metatable, you should not change the value of its
field __mode. Otherwise, the weak behavior of the tables controlled by this
metatable is undefined."

So I think setting the metatable to nil of a weaktable should be a valid
operation)

The second way (using a non-weak table to store the userdatas) should work
without problems.

Which way would you suggest to use? Performance is the key here, so it looks
like that setting metatable of the weak table should be somewhat better than
the second approach - but I don't know what is happening in the VM if the
metatable of a weak table is changed quite often. Any drawbacks here?

Thanks!
Eike