Program end and __gc

classic Classic list List threaded Threaded
3 messages Options
Reply | Threaded
Open this post in threaded view
|

Program end and __gc

Krunal Rao
Let's assume that we have some userdata objects with a __gc metamethod which are
still alive at the end of the execution of a "main" Lua script.

1. Is it guaranteed that the still alive objects will be garbage collected and
the __gc metamethod invoked on them (barring "mutual referencing" situations
where such objects cannot be collected) ?

2. Is it fine to invoke assert or error from the code inside __gc?

An example could be the file handle from the io library.

Thanks!


Reply | Threaded
Open this post in threaded view
|

Re: Program end and __gc

Luiz Henrique de Figueiredo
> 1. Is it guaranteed that the still alive objects will be garbage collected and
> the __gc metamethod invoked on them

Yes, if the script ends normally, because lua_close will be called.

> (barring "mutual referencing" situations
> where such objects cannot be collected) ?

*All* objects will be collected when the state is closed.

Reply | Threaded
Open this post in threaded view
|

Re: Program end and __gc

Krunal Rao
Thank you!

On 31 October 2011 16:33, Luiz Henrique de Figueiredo
<[hidden email]> wrote:

>> 1. Is it guaranteed that the still alive objects will be garbage collected and
>> the __gc metamethod invoked on them
>
> Yes, if the script ends normally, because lua_close will be called.
>
>> (barring "mutual referencing" situations
>> where such objects cannot be collected) ?
>
> *All* objects will be collected when the state is closed.
>
>