lua-users home
lua-l archive

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

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.


- 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: 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 │┈┈┈┈┈│ approximations to perfectly elastic spherical
│┈(┈│ cows." --- David M. Palmer on r.a.sf.c