Throwing an error within a __gc metamethod

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

Throwing an error within a __gc metamethod

Fotis Panagiotopoulos
Hi,

I was checking at the documentation, but I couldn't find any information on this.

Is it legal to throw an error (e.g. call luaL_error(L, "some error")) within a __gc metamethod?

What will happen to the table in this case, will it be free'd correctly? And especially what will be the behavior of lua_close(L)? Will the finallization of the Lua state be complete, or the error will interrupt it?
Reply | Threaded
Open this post in threaded view
|

Re: Throwing an error within a __gc metamethod

Roberto Ierusalimschy
> Is it legal to throw an error (e.g. call luaL_error(L, "some error"))
> within a __gc metamethod?

Yes, although not a good practice.


> What will happen to the table in this case, will it be free'd correctly?
> And especially what will be the behavior of lua_close(L)? Will the
> finallization of the Lua state be complete, or the error will interrupt it?

When closing a state, all finalizers are called in protected mode.
Therefore, any errors there only interrupt that specific finalizer;
everything else goes on normally. (In particular, the table itself is
free'd correctly. The two operations are unrelated.)  Until 5.3, these
errors are just ignored. Lua 5.4 will have a warning system that will
generate warnings in this case (without changing the current behavior).

-- Roberto