lua-users home
lua-l archive

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



On 28/9/22 12:56, Lorenzo Donati wrote:

On 28/09/2022 16:24, Jorge Visca wrote:
On 28/9/22 04:25, Francisco Olarte wrote:
They are just a variation of try/finally, similar to
try-with-resources, not that difficult to understand.

My problem was that in my mind "going out of scope" was "going out of scope everywhere in the program". Yes I see that that's not how Lua's gc works.

"going out of scope everywhere in the program": are you sure you are not conflating/confusing scope with lifetime?

Yes, that was what I was doing.


You seem to imply that you want to clean-up library resources (automatically) just when the program terminates (otherwise I cannot understand what you mean by "going out of scope everywhere in the program").

The garbage collector guarantees that when a program terminates any object marked for finalization will have their __gc metamethods called. So any object created in a library, if you give it a __gc MM, will get a chance to free their acquired resources, too.

Effectively I wanted it to be garbage collected as soon as possible, as if it was ref-counting. For context, I wanted my library to keep only weak references to the (expensive) objects it produces, plus no explicit "release" library methods.

Thinking a bit more, in some cases I still can use <close> to obtain something like this, as long as the library's API keeps de <close> variables inside the library.


Jorge