lua-users home
lua-l archive

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



On Nov 25, 2006, at 6:09 PM, Ram Firestone wrote:

__gc only works for userdata values.


Ah, I was working off the info at:

http://lua-users.org/wiki/MetatableEvents

That stinks. There's a lot of busy-work in C that I'd rather do in Lua. Too bad.

Thanks for the correction.

-joe

-----Original Message-----
From: lua-bounces@bazar2.conectiva.com.br [mailto:lua- bounces@bazar2.conectiva.com.br] On Behalf Of
joseph stewart
Sent: Saturday, November 25, 2006 3:05 PM
To: Lua list
Subject: Can I use __gc metamethod directly from Lua?

Hello all,

I'm developing a library binding and to minimize the amount of 'C'
code I need to write, I want to have Lua handle the __gc metamethod.
At first glance, this doesn't appear to do what I want. Here's the
essence of what I'm doing:

mt = {}
mt.__gc = function() print("gc me") end
t = {1,2,3}
setmetatable(t, mt)
t = nil
collectgarbage("collect")

I would expect to see "gc me" printed after the collectgarbage
function call, but I don't.

Am I completely on the wrong track? I've used __gc from 'C' code and
more or less get what I expect. Is __gc usable from Lua code?

Thanks in advance for any help! When the library is done, I'll share
it with y'all.

-joe