[Date Prev][Date Next][Thread Prev][Thread Next]
- Subject: Re: state of the Lua nation on resource cleanup
- From: Roberto Ierusalimschy <roberto@...>
- Date: Tue, 10 Feb 2009 21:07:31 -0200
> Besides opinions from the community, I'm hoping for some word from the
> Lua authors like one of "working on a solution", "Lua doesn't need
> improvement here", "see the need for it but don't have a design in
> mind", etc.
Later on this discussion David Manura wrote:
> Now, personally, in practice, I've only found myself wanting this time
> of capability in Lua on a few occasions (but perhaps others with
> different applications will have greater need for it):
> - closing file handles upon exceptions
> - closing COM objects on exceptions
> - releasing native OS handles on exceptions, e.g. as once in
> - and if I used Lua more for database and socket work, quite
> possibly that too.
I personaly agree with him. In all cases the key point is "on exceptions".
He then goes on:
> Workarounds I've used are explicit collectgarbage() calls judiciously
> placed, adding pcalls (often ugly, affects error tracebacks, and can
> conflict with coroutines), and ignoring the problem (sometimes
Maybe we could avoid these workarounds by forcing a full garbage collection
upon errors. This could be added to the core or through the libraries.
(That is, pcall and other library functions that do protected calls should
call lua_gc in case of errors, and user's C code would be recommended
to do the same.)
That is, all resources should have an explicit "close" method plus
a finalizer that releases it. (Like files from the io library.) In
regular use, the control flow should call the close method for prompt
release. On exceptions, a full collection would call the finalizers.
If needed, we could add to the library a "generic resource":
a = newresource(func) -- creates something
a:close() -- calls 'func'
If 'a' is collected before being closed, its finalizer calls 'func'.