lua-users home
lua-l archive

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


On May 30, 2009, at 1:19 AM, Cosmin Apreutesei wrote:

That being said, I'd dare making a proposal that I've been giving some
thought, maybe I can hear your opinions about it (well, aside from the
trouble it would mean to implement it): how about an __immediate_gc
metamethod that would ensure finalization of a table or userdata when
the last reference goes out of scope.

That's what the various RAII/scoped/with/etc techniques endeavor to do. However, they can't tell when the last reference goes out of scope because the only way to know that is to do a full GC pass. What they allow the programmer to do is to specify that the lifetime of a particular reference is at least as long as that of any other references and hence when that reference goes out of scope, the object can be cleaned up. The clean up leaves the object in a technically valid state so there isn't a security hole/crash waiting to happen if the expectations about lifetime were invalid -- e.g., we stuffed the value into a global -- but usage may result in an error being thrown.

Mark