[Date Prev][Date Next][Thread Prev][Thread Next]
[Date Index]
[Thread Index]
- Subject: Re: userdata and the gc
- From: Chris Marrin <chris@...>
- Date: Wed, 19 Apr 2006 07:24:22 -0700
Adrian Sietsma wrote:
...
Chris :
I'm not sure if Roberto intended what you think you read. If and when
Lua gets a compacting garbage collector, a userdata's address would
move around just like any other gc object. Maybe that will never
happen and maybe they have some clever way to guarantee that userdatas
never move. But it seems like a better idea to not rely on this and
use your own data.
I read A to mean that userdata never moves, and will code accordingly.
It would be nice to have this point clarified in the manual.
ps
consider - if a userdata can move due to garbage collection, it would
have _major_ implications for c routines. There would need to be clear
rules on the lifetime of pointers returned from lua_newuserdata() and
lua_touserdata(), and which operations may invalidate them. Or, some
contract with the gc to say "leave this block alone".
I think it would be a mistake not to write code protecting your C
functions from disappearing userdata, which by implication can protect
you from moving userdata as well. I have had places in my code where I
maintained pointers to userdata and it inevitibly caused problems. Now I
let Lua maintain the pointers and I simply have references into the Lua
VM. I either use the registry or similar tables. The __gc metamethod can
keep you fully informed about the lifetime of each userdata. And Lua has
a powerful feature for maintining weak pointers to avoid frozen userdata
pointers. When attempting to get a reference to a weak pointer you will
get back a nil so you can cleanly handle this case. All these techniques
keep your C state consistent with your Lua state.
I think one of Lua's most powerful and useful features is its simple yet
flexible C interface.
--
chris marrin ,""$, "As a general rule,don't solve puzzles
chris@marrin.com b` $ that open portals to Hell" ,,.
,.` ,b` ,` , 1$'
,|` mP ,` :$$' ,mm
,b" b" ,` ,mm m$$ ,m ,`P$$
m$` ,b` .` ,mm ,'|$P ,|"1$` ,b$P ,` :$1
b$` ,$: :,`` |$$ ,` $$` ,|` ,$$,,`"$$ .` :$|
b$| _m$`,:` :$1 ,` ,$Pm|` ` :$$,..;"' |$:
P$b, _;b$$b$1" |$$ ,` ,$$" ``' $$
```"```'" `"` `""` ""` ,P`