lua-users home
lua-l archive

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


 > > I've scrutinized the reference manual and also done some web searching,
 > > and nowhere do I find the answer to this question: if Lua exits either by
 > > a call to os.exit or by a normal return from the main chunk, does it
 > > guarantee to call the __gc finalizers for any live userdata objects?
 > 
 > No and yes. The 'lua' applicative calls lua_close before returning,
 > and so clears everything. A call to os.exit skips that closing. We
 > thought about adding lua_close in an atexit function, but leaved as
 > it is so there is a way to skip lua_close if needed. (This may speed
 > up some programs which finish with lots of data to clean up.)

Hmm.  I wouldn't like to make that decision myself without some
experimental evidence.

So suppose I do the following:

  local exit = os.exit
  local pairs = pairs
  local insert = table.insert
  local collectgarbage = collectgarbage

  local function wipe(t)
    local keys = { }
    for k in pairs(t) do insert(keys, k) end
    for _, k in pairs(keys) do t[k] = nil end
  end

  os.exit = function(n) wipe(_G); collectgarbage 'collect'; exit(n) end

I still worry that there may be live variables on the stack that don't
get finalized.   Any ideas what I can do about that?

It's annoying that a function passed to atexit() won't take a parameter!
I assume this is why there's no os.atexit in Lua...


Norman