lua-users home
lua-l archive

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


Hi Viacheslav,

> This may have  been discussed earlier; if so, kindly point me to the previous discussions.

There have been several related discussions/write-ups, for example, on
using finalize/guard
(http://lua-users.org/lists/lua-l/2008-02/msg00243.html) and
deterministic cleanup
(http://john.neggie.net/2009/lua/resource_finalization).

Paul.

On Fri, Nov 13, 2015 at 2:19 AM, Viacheslav Usov <via.usov@gmail.com> wrote:
> This may have  been discussed earlier; if so, kindly point me to the
> previous discussions.
>
> Lua uses garbage collection for everything. User-defined objects can use
> metatables to perform appropriate finalization. For example, Lua's built-in
> io library use the __gc field in its files' metatable to close the
> underlying OS file, even if the user does not call the file:close function.
> That works very well, except that finalization is non-deterministic, i.e.,
> it is impossible to predict when it will happen.
>
> Using the example of files, this may be problematic, because the file
> remains open unpredictably long, which may interfere with the other uses of
> the file. It could be said that if determinism is important, the user must
> ensure that file:close is called. Unfortunately, taking into account that
> there can be some very complicated logic between io.open and file:close,
> which may also raise Lua errors, this could lead to extremely unwieldy code.
>
> This problem is not specific to Lua and probably exists in every
> GC-collected environment, so there are some established ways of dealing with
> it. In C#, for example, this is done via the keyword 'using', which
> establishes a scope, upon exiting which (including exiting via an
> exception), the object is finalized. Example from
> https://msdn.microsoft.com/en-us/library/yh598w02.aspx
>
> using (Font font1 = new Font("Arial", 10.0f))
> {
>     byte charset = font1.GdiCharSet;
> }
>
>
> Syntactically, this could be transferred to Lua using a new keyword, such as
> block, instead of local, to define a local variable that must be finalized
> when exiting the block (including error exits). The finalization will use
> the __gc methamethod, if present, and subsequent garbage collection proceeds
> in the usual manner. For example:
>
> block file = io.open('file')
>
> The use of a new keyword seems necessary so that the user could control
> whether this block-scope finalization is to happen, because it will be
> unsuitable when the object is supposed to outlive its block. This could
> probably be also controlled by some clever metatable manipulation; but that
> will make it an 'advanced' feature, limiting its adoption.
>
> Comments?
>
> Cheers,
> V.