[Date Prev][Date Next][Thread Prev][Thread Next]
- Subject: Re: Can I use __gc metamethod directly from Lua?
- From: joseph stewart <joseph.stewart@...>
- Date: Sat, 25 Nov 2006 19:15:27 -0500
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
world of pain, I'm afraid. Consider the following:
Okay, I give. I'll use userdata. Thanks for the feedback.
- what happens if the garbage collector runs during execution of
- what happens if your finaliser tries to make the object live
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?
file handles. I've been bitten by this.)
- what happens if your program terminates in such a way that Lua
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:
Lua avoids this kind of nasty mess by decreeing that finalisers are
available from the Lua language. Finalisers only work on userdata
you may only set the finalisers from C, which means you are forced
about what you're doing and be aware of the issues.
In general, using finalisers to do anything other than to
resources such as memory is generally considered a really bad idea.
to say that can't also be useful, but, well, You Have Been Warned...
╭─┈David Given┈──McQ─╮ "...electrons, nuclei and
other particles are good
│┈ firstname.lastname@example.org┈┈┈┈│ approximations to perfectly
│┈(email@example.com)┈│ cows." --- David M. Palmer on r.a.sf.c