|
On Mar 30, 2007, at 11:09 AM, Tomas Guisasola Gorham wrote:
I think it is best to just keep track of the state of your userdata and any associated resources. For example: if your userdata is a struct, add a field to mark when it is finalized. If your userdata is a boxed pointer, make sure you nullify the pointer when you are done with it.We do exactly this in the implementation of LuaSQL.Yes, I've done this now... but still I think __gc should be private by default.In LuaSQL, the `close' method and the `__gc' metamethod are the same C function. Do you have a better idea?
Well there's no harm having a close method; so long as it makes sense to retain a LuaSQL userdata reference that is no longer connected. Otherwise, I thought setting the userdata to nil would be the equivalent.
I thought that __gc was a callback to let you know that the userdata is no longer referenced in Lua and will be deleted. But having it as a public method, simply means that __gc is a callback to say that either the userdata is no longer referenced, or someone called __gc (); it's less well defined.