Calling a function on completion / exit from a lua script (atexit () / cleanup function)
Question: Is there a way to call a function when a lua script is terminated either by the system or by the program that launched the script (like a C program)? Atexit () / cleanup function for lua.
Situation: an external C program (calling PROG) controls a lua script (calls it SCRIPT) and calls its functions, the lua script uses a shared library (.so, cal it LIB), which reserves resources to be freed when the lua script exits. The Lua script is driven (and thus ends up) with PROG, which I cannot change. The script should notify the LIB when it's finished.
How can I do that? Note: I am pretty new to lua, so please explain your answer, greatly appreciate :)
I am on Linux using Lua 5.3.1
This currently works:
a = {__gc = function() print'exit function from LIB called' end}
setmetatable(a,a)
source to share
Check out http://lua-users.org/lists/lua-l/2001-08/msg00265.html
You may need to run a lua_runprotected call to prevent a thread in the thread problem.
source to share