lua-users home
lua-l archive

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

On Wed, 28 Sept 2022 at 09:41, bil til <> wrote:
> > > And then it really would be much better, if the __close metafunction
> > > is used automatically (if it exists).
> > You have one of these, it is called __gc.
> Thank you, this is interesting remark, also this sort of "competition"
> between __close and __gc.

There is no competition, they are mechanism, is up to you to use it
properly. As I said somewhere else, I normally use both redirecting
them to an idempotent cloe() method.

> Would __gc be invoked for local variables or not?

You have a problem thinking off variables/values here.  If you omit
the uninteresting ( from a gc/closing perspective ) types ( nil,
boolean, numbers, lightuserdata and strings ) and center on the
interesting ones ( tables/fulluserdata ) local variables in lua are a
pointer, or a binding, call it what you like to the real value. And
other things which refer to them, like uservalues, or table entries,
are more or less the same. GC is called on VALUES. It does not care
where you store pointers to it, it cares whether there are live
pointers or not. Also, as described, if you have coroutine problems
your variables may not be closed, but the values they point to may be
collected ( if the coroutine is ).

Close is a bit different, as it deals with the local variables. Local
variables go out of scope on a defined way, and lua calls _close on
them if needed, but it does not know if the object is properly
disposed. You can easily get a <close> local and store its value in a
global, to close will be called on them at scope exit, but _gc will
not be called until you clear the global ( and gc detects that ),
because _closing does not mean the value is done. You can easily
manage to get _close called a lot of times, just put the some object
in several <close> or in the same several times in a loop or sequence.
That's why I make sure my close() methods are idempotent.

> In the Reference Manual the local variables are described in §3.2, but
> their closing behaviour is a bit unclear I think. I cannot really
> recognize, whether local variables are destroyed by gc, or whether
> they exist on stack and are destroyed "automatically" and end of their
> scope?

In the manual "to be closed" are described by 3.3.8, but you have a
concept problem here the Garbage Collector, GC, destroys VALUES not
variables. Local variables, the only ones that really exist in lua as
globals are just entries in a table, reference values ( forbidding the
GC from collecting them ) and call __close if needed and get destroyed
( allowing the GC to collect the value if it needs so ).

Lua makes it easy to call everything a variable, which is great for
simple stuff, but once you get into lifetimes, resource management and
other complex things you need to go deeper and think on values and
references, it gets more complex. All languages do get more complex
when you get to do complex stuff.

Francisco Olarte.