lua-users home
lua-l archive

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



On 23-Sep-06, at 1:59 AM, Glenn Maynard wrote:

I think Lua's GC scheme isn't capable of actually implementing dtors
efficiently, but how do people deal with this?  I can't just force a
complete GC cycle; aside from performance, that's just the "close"
problem in another form.

Garbage collectors aren't really designed to handle this problem.

Here's something I've been playing with. I'm not crazy about the extra argument to the protected function; I could have gotten around that by stashing the finalizers in the environment (since I can reset the finalizers array on the way out) but that's not guaranteed to work if people mess around with function environments.

----

local function collect(finalizers, ind, ...)
  for i = #finalizers, 1, -1 do pcall(finalizers[i]) end
  return ind, ...
end

local function addf(self, func, targ)
  self[#self+1] = function() func(targ) end
end

function protect(f, ...)
  local finalizers = {add = addf}
  return collect(finalizers, pcall(f, finalizers, ...))
end


function close(x) print("Closing: ", x) end

function doSomeWorkBadly(finally, a, b)
  local c = "some precious resource"
  finally:add(close, c)
  print("a+3 is ", a+3)
  local d = "another precious resource"
  finally:add(close, d)
  print("b+3 is ", b+3)
  finally:add(print, "Almost done")
  return a+b
end