lua-users home
lua-l archive

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


Put a userdata with GC method in your table. Should have
the same effect.

joseph stewart wrote:
> I'm just curious. Why do you need a __GC method for a table anyway?
> I tend to always use this for C++ classes where I need to call
> the destructor.
>

As I mentioned, I'm trying to move some of the gruntwork from the 'C'
side to the Lua side. Without going into too many details, I have low-
level "allocation" functions callable by Lua that have items that
need corresponding "de-allocate" functions to avoid memory leaks. My
Lua-side code handled creating tables that I'd hoped could "self-
clean" during the collection cycle.

I'm familiar with doing this on the 'C' side, I was just trying to
minimize the 'C' code.

-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:15 PM
>> To: Lua list
>> Subject: Re: Can I use __gc metamethod directly from Lua?
>>
>>
>> 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
>>>
>