lua-users home
lua-l archive

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


So, skipping through liolib.c, in summary, in addition to deleting object boxed, my __gc method should:

set the boxed pointer value to NULL

and my type-checking code should:

test for userdata, 
validate the userdata against the type (e.g. test metatable)
validate that the boxed pointer is not NULL

Setting the pointer to NULL, and testing for NULL, would not be necessary if __gc was not visible to Lua code, and if the lifetime of the object boxed is known to be equal to the lifetime of the userdata, a common case.  

I can't think of an example why you would want to call __gc from Lua code; that's why I'm surprised that it is visible at all.  

Even in liolib.c, where the object boxed (the file) is not necessarily concurrent in existence with the userdata, there is still no need to access __gc from Lua.

On Mar 30, 2007, at 4:51 AM, Luiz Henrique de Figueiredo wrote:

so any __gc metamethod in C must typecheck the argument.

Yes. And in the case of boxed pointers, invalidate it somehow.
That's what the io lib does. (But I'm afraid not all my libraries do it.)
Bottom line: you have to write C binding code carefully.
--lhf

grrr waaa
www.grahamwakefield.net