lua-users home
lua-l archive

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


On Wed, Jun 24, 2020 at 7:26 AM Roberto Ierusalimschy <roberto@inf.puc-rio.br> wrote:
> Having finalizers to back up __close solves all these cases, where an
> object goes out of existence without going out of scope. The manual
> hints about this, but it could be more emphatic.
>

This will release resources such as file handles just fine. The nice thing about __close is that it's pretty deterministic, like a C++ destructor, the C# using() block, or the Golang "defer" statement. You know when it will be called, whereas __gc can be called whenever the VM or a C routine allocates memory, so you have to be very careful about touching anything outside of the object being garbage collected. The deterministic behavior makes __close useful for more than closing file handles and such.

> The goal of <close> inside coroutines + coroutine.close is to allow
> orderly kills of threads/coroutines in Lua.

It took me a little while to figure out a strategy that allows that orderly kill of any coroutine left behind by an error in a program.
Perhaps this is a subject for a new chapter in an updated edition of you Lua book😀

Thanks,


--