Can I use __gc metamethod directly from Lua?

classic Classic list List threaded Threaded
13 messages Options
Reply | Threaded
Open this post in threaded view
|

Can I use __gc metamethod directly from Lua?

Joseph Stewart
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

Reply | Threaded
Open this post in threaded view
|

RE: Can I use __gc metamethod directly from Lua?

Ram Firestone
__gc only works for userdata values.

> -----Original Message-----
> From: [hidden email] [[hidden email]] 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


Reply | Threaded
Open this post in threaded view
|

Re: Can I use __gc metamethod directly from Lua?

Michael Broughton
In reply to this post by Joseph Stewart
The __gc meta-method only works on full userdata. It does not get called for regular Lua tables.

Mike


joseph stewart wrote:
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


Reply | Threaded
Open this post in threaded view
|

Re: Can I use __gc metamethod directly from Lua?

Joseph Stewart
In reply to this post by Ram Firestone

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: [hidden email] [[hidden email] [hidden email]] 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



Reply | Threaded
Open this post in threaded view
|

RE: Can I use __gc metamethod directly from Lua?

Ram Firestone
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.  

> -----Original Message-----
> From: [hidden email] [[hidden email]] 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: [hidden email] [[hidden email]
> >> [hidden email]] 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
> >


Reply | Threaded
Open this post in threaded view
|

Re: Can I use __gc metamethod directly from Lua?

Joseph Stewart
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: [hidden email] [[hidden email] [hidden email]] 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: [hidden email] [[hidden email]
[hidden email]] 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




Reply | Threaded
Open this post in threaded view
|

Re: Can I use __gc metamethod directly from Lua?

D Burgess-4
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: [hidden email] [[hidden email]
>> [hidden email]] 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: [hidden email] [[hidden email]
>>>> [hidden email]] 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
>>>
>



Reply | Threaded
Open this post in threaded view
|

RE: Can I use __gc metamethod directly from Lua?

Ram Firestone
In reply to this post by Joseph Stewart
I'm still not sure of your application; however tables will "self clean" on their own. The __GC method just doesn't get called on
them because it's not necessary. 

Perhaps you are storing userdata items in your table? If this is the case the __GC method will be called for each userdata item in
your table separately (assuming it has one) before the table is destructed. So you can do your memory clean up there. 

I'll just assume you are doing something that is beyond my experience because other than some garbage collection tracking
application I don't see a reason for __GC method on a table. I'm not even sure what code you would put there.

> -----Original Message-----
> From: [hidden email] [[hidden email]] On Behalf Of
> joseph stewart
> Sent: Saturday, November 25, 2006 3:35 PM
> To: Lua list
> Subject: Re: Can I use __gc metamethod directly from Lua?
> 
> > 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: [hidden email] [[hidden email]
> >> [hidden email]] 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: [hidden email] [[hidden email]
> >>>> [hidden email]] 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
> >>>
> >


Reply | Threaded
Open this post in threaded view
|

Re: Can I use __gc metamethod directly from Lua?

David Given
In reply to this post by Joseph Stewart
joseph stewart wrote:
[...]
> I'm familiar with doing this on the 'C' side, I was just trying to
> minimize the 'C' code.

Trying to do real work from a finaliser in Lua land is going to involve a
world of pain, I'm afraid. Consider the following:

 - what happens if the garbage collector runs during execution of your finaliser?
 - what happens if your finaliser tries to make the object live again by
making a reference to it?
 - what happens if an error occurs during execution of the finaliser?
 - what happens if the garbage collector doesn't run often enough, and you run
out of whatever resource you're keeping track of in your finaliser? (Example:
file handles. I've been bitten by this.)
 - what happens if your program terminates in such a way that Lua doesn't
finalise things? (Sounds unlikely, but it can happen.)

Just take a look at the horrible nightmare that is Java finalisation as an
example --- in particular, look at the state machine diagram here:

http://java.sun.com/docs/books/jls/second_edition/html/execution.doc.html#44760

Lua avoids this kind of nasty mess by decreeing that finalisers are not
available from the Lua language. Finalisers only work on userdata objects, and
 you may only set the finalisers from C, which means you are forced to think
about what you're doing and be aware of the issues.

In general, using finalisers to do anything other than to deallocate trivial
resources such as memory is generally considered a really bad idea. That's not
to say that can't also be useful, but, well, You Have Been Warned...

-- 
âââDavid GivenâââMcQââ "...electrons, nuclei and other particles are good
ââ [hidden email]âââââ approximations to perfectly elastic spherical
ââ([hidden email])ââ cows." --- David M. Palmer on r.a.sf.c
âââwww.cowlark.comââââ

Attachment: signature.asc
Description: OpenPGP digital signature

Reply | Threaded
Open this post in threaded view
|

Re: Can I use __gc metamethod directly from Lua?

Joseph Stewart

On Nov 25, 2006, at 7:09 PM, David Given wrote:

joseph stewart wrote:
[...]
I'm familiar with doing this on the 'C' side, I was just trying to
minimize the 'C' code.

Trying to do real work from a finaliser in Lua land is going to involve a
world of pain, I'm afraid. Consider the following:


Okay, I give. I'll use userdata. Thanks for the feedback.

-j

- what happens if the garbage collector runs during execution of your finaliser? - what happens if your finaliser tries to make the object live again by
making a reference to it?
 - what happens if an error occurs during execution of the finaliser?
- what happens if the garbage collector doesn't run often enough, and you run out of whatever resource you're keeping track of in your finaliser? (Example:
file handles. I've been bitten by this.)
- what happens if your program terminates in such a way that Lua doesn't
finalise things? (Sounds unlikely, but it can happen.)

Just take a look at the horrible nightmare that is Java finalisation as an
example --- in particular, look at the state machine diagram here:

http://java.sun.com/docs/books/jls/second_edition/html/ execution.doc.html#44760

Lua avoids this kind of nasty mess by decreeing that finalisers are not available from the Lua language. Finalisers only work on userdata objects, and you may only set the finalisers from C, which means you are forced to think
about what you're doing and be aware of the issues.

In general, using finalisers to do anything other than to deallocate trivial resources such as memory is generally considered a really bad idea. That's not
to say that can't also be useful, but, well, You Have Been Warned...

--
âââDavid GivenâââMcQââ "...electrons, nuclei and other particles are good ââ [hidden email]âââââ approximations to perfectly elastic spherical
ââ([hidden email])ââ cows." --- David M. Palmer on r.a.sf.c
âââwww.cowlark.comââââ




Reply | Threaded
Open this post in threaded view
|

Re: Can I use __gc metamethod directly from Lua?

Adam D. Moss
In reply to this post by David Given
David Given wrote:
Trying to do real work from a finaliser in Lua land is going to involve a
world of pain, I'm afraid. Consider the following:
...
Lua avoids this kind of nasty mess by decreeing that finalisers are not
available from the Lua language.

Well... having asked about this before, I got the reply that using a
Lua function as a finalizer is fully supported, IIRC.  Certainly I do
this extensively without conspicuous problems.

--Adam

Reply | Threaded
Open this post in threaded view
|

Re: Can I use __gc metamethod directly from Lua?

David Given
Adam D. Moss wrote:
[...]
> Well... having asked about this before, I got the reply that using a
> Lua function as a finalizer is fully supported, IIRC.  Certainly I do
> this extensively without conspicuous problems.

Yeah, but setmetatable() doesn't work on userdata, and __gc *only* works on
userdata, which means that in order to do it you have to go via C. Which means
you have to be really serious to do it.

IIRC, Lua follows similar semantics to Java --- finalisers are only ever
called once, which means that if an object is resurrected and then garbage
collected again, it won't be finalised again. (Now I think of it, if your
finaliser is running Lua code it must be briefly resurrected, since 'self'
must be referenced.)

-- 
âââDavid GivenâââMcQââ "...electrons, nuclei and other particles are good
ââ [hidden email]âââââ approximations to perfectly elastic spherical
ââ([hidden email])ââ cows." --- David M. Palmer on r.a.sf.c
âââwww.cowlark.comââââ

Attachment: signature.asc
Description: OpenPGP digital signature

Reply | Threaded
Open this post in threaded view
|

Re: Can I use __gc metamethod directly from Lua?

Carol Hurwitz
In reply to this post by Adam D. Moss
I would like to be removed from this list for the time being.
Thank you.
Carol Hurwitz