lua-users home
lua-l archive

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

Am 14.03.2014 14:08 schröbte Coroutines:
On Fri, Mar 14, 2014 at 6:06 AM, Coroutines <> wrote:
On Fri, Mar 14, 2014 at 4:53 AM, Luiz Henrique de Figueiredo
<> wrote:
a = {__gc = function() print'exit' end}

-> nothing to print.
But if i exit with combination <Ctrl+d>  all Ok.

os.exit() terminates the process there and then.

<Ctrl+d> terminates the session cleanly, calling lua_close,
which then calls the GC methods.

see I would have expected the opposite... because in C you have exit()
and at_exit(), I thought lua_close() would be called at_exit() by the
interpreter.  Doing ctrl + d would be more "instantaneous"?  Kinda
wish both had called the __gc eitherway ~

Ctrl+d just closes the input stream. It is up to the program how to react to that. Lua chooses to shutdown gracefully. `exit()` on the other hand terminates the process via a system call without any cleanup (e.g. C++ destructors) except exit handlers.

er, I mean atexit(), not at_exit().  I wish the standard lua
interpreter (embedding example?) registered a function to lua_close()
the main state with atexit()

There can be multiple main states. `atexit()` takes `void (*)(void)`, so you would need to manage a list of Lua states as a global variable. `atexit()` also cannot portably be used when the cleanup handler is in a dynamically loaded shared libary (some libc implementations also scan the exit handler list when a library is unloaded, some don't).

So `exit()` and `atexit()` stopped being useful a long time ago, at least for reusable code. (`os.exit()` is the only way to pass a specific exit status from within Lua, though.)