lua-users home
lua-l archive

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


Having a close() method may be a good idea for many uses of userdata, where freeing resources referred by the userdata can be independent of the userdata lifetime.  In such cases, it makes sense to test against NULL for the userdata contents, etc.  Calling udata:close() won't set the udata to nil, won't free the userdata itself, since this is under the control of the garbage collector.

But this is irrelevant to __gc.  The __gc metamethod, as far as I understand, is supposed to be a kind of callback into C to let you know that the userdata has been collected, so you can free additional resources if necessary.  It makes no sense to expose this method to Lua code.   

Using __gc as a close() method confuses two distinct purposes.

On Mar 30, 2007, at 6:01 PM, Javier Guerra wrote:

On Friday 30 March 2007, Graham Wakefield wrote:
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.

sometimes you want an immediate close, not wait until the GC decides it needs 
to collect the userdata.

also, remember that the GC only counts the memory used by the userdata itself; 
the backend library might use a lot more resources unaccounted by the GC.

usually having a close() is a good idea

-- 
Javier

grrr waaa
www.grahamwakefield.net