lua-users home
lua-l archive

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



On 16-May-05, at 10:43 PM, Chris Marrin wrote:

Rici Lake wrote:

...
If you don't want to go hacking the Lua core, I have another solution... You say you're reffing the new userdata anyway; this is going to store a reference in the registry. There's no magic to luaL_ref other than that. So why not kill two birds with one stone? Instead of using luaL_ref to store a reference in the registry, store your own reference in the registry using a lightuserdata of the this pointer as a key. Then you can push the lua object onto the stack using the this pointer, without breaking the API and without incurring much overhead (since the hash of a lud is essentially the address).

The problem with that is now I would always have a pointer to the userdata and it would never GC, right? When I used my own table to store the lightuserdata key/userdata value pair, I made the values in that table weak so they would get collected properly.

Is there another way to avoid the GC problem?

One of us is not being clear :)

You said:

This leaves the userdata on the stack, so my Object ctor fiddles with it (gives it a common metatable and refs it so it doesn't get GC'ed). Then it gets popped.

I assumed you meant by "refs it" that you used luaL_ref to create a reference to it so that it *wouldn't* be collected. Now you seem to be saying that you *want* it to be collected.

You could do either of those things, as Mark points out, by using both a weak and a strong table with the weak table backing the strong table via an __index meta

But perhaps I still don't understand...