lua-users home
lua-l archive

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


2016-03-21 23:34 GMT+02:00 Tim Hill <drtimhill@gmail.com>:
> (Apologies for the longish post…)

Summary: in the following scenario:
   1. Lua calls C;
   2. C creates some persistent structure, which
needs a later C call to be properly cleaned up;
   3. Something in Lua throws an error (which
is implemented as a longjmp);
the cleanup may be never called, or at best called
via __gc at an unpredictable time.

Isn't this situation best handled by making C the boss?
I.e. write your own host program, making liberal use
of the code in lua.c. Register your structure with the host,
which still has control when some LuaState in it is terminated.

Thing is, your host can be aware of whatever quirks your
structure has, whereas whatever way the Lua API has of
handling it must be abstracted (the __gc mechanism being
the current abstraction).

You mention C++. I am not sure how C++ does it, but
when I wrote a C Lua binding for GNU APL 1.5 about
a year ago, I found that I did not need to worry about
the APL workspace: it was somehow taken care of
automatically when Lua exited. I _think_ the workspace
is a small static structure that is initialized at load time,
so maybe it is some (probably non-portable) feature
of package.loadlib.