lua-users home
lua-l archive

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



RL>> You could just attach the __gc method to the
RL>> userdata and just use it rather than wrapping it.


TW> I don't think that would work in this case. Remember the original
TW> purpose was to allow a resource to be "deallocated" either manually
TW> using some kind of delete function or automatically if it wasn't done
TW> manually. If the tag was connected to the userdata __gc method it must
TW> either be able to tell that it has been deallocated (by the manual
TW> method) or it might try to deallocate it twice. Might work fine in some
TW> applications, but not in general.

Fair enough. The manual delete method would have to leave some sort of
history behind; if the metatable were uniquely generated for each such
object (probably not awful, given the limited number of such objects), then
it could simply delete the __gc method from the metatable. Otherwise, it
would have to flag the userdata itself. That shouldn't be too difficult,
since either the userdata will consist solely of a pointer to some opaque C
structure and that pointer could be set to NULL, or the userdata will
contain the field of the structure itself, in which case a flag can be
added. I don't think a weaktable solution exists for external marking, but
it would be possible to use a strong table with a hidden pointer; the key
would be a lightuserdata containing the address of the userdata, and the
__gc method would have to manually delete the key.