userdata environment garbage collection

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

userdata environment garbage collection

Timothy Hunter
I'm trying to understand how garbage collection works for userdata environments.

I allocate a userdata and then call lua_setfenv to set an environment table for it. Then I allocate a 2nd userdata and store it in the environment table. Eventually Lua collects the 1st userdata.

I assume that the 2nd userdata will not be collected until the reference in the environment table goes away. Is that correct? Do I need to do anything to allow the environment table to be collected? After the environment table is collected,  do I need to do anything to allow the 2nd userdata to be collected?

--
Tim
Reply | Threaded
Open this post in threaded view
|

Re: userdata environment garbage collection

Sam Roberts
On Thu, Mar 24, 2011 at 5:21 PM, Timothy Hunter <[hidden email]> wrote:
> I'm trying to understand how garbage collection works for userdata environments.
>
> I allocate a userdata and then call lua_setfenv to set an environment table for it. Then I allocate a 2nd userdata and store it in the environment table. Eventually Lua collects the 1st userdata.
>
> I assume that the 2nd userdata will not be collected until the reference in the environment table goes away. Is that correct? Do I need to do anything to allow the environment table to be collected?

No, not if the only ref to the env table was from userdata1.

> After the environment table is collected,  do I need to do anything to allow the 2nd userdata to be collected?

Everything should work out correctly, this is pretty much what
userdata envs are for. One thing, anyhow.

Sam

Reply | Threaded
Open this post in threaded view
|

Re: userdata environment garbage collection

Roberto Ierusalimschy
In reply to this post by Timothy Hunter
> I allocate a userdata and then call lua_setfenv to set an environment
> table for it. Then I allocate a 2nd userdata and store it in the
> environment table. Eventually Lua collects the 1st userdata.
>
> I assume that the 2nd userdata will not be collected until the
> reference in the environment table goes away. Is that correct?

Mostly, but there is one detail: when the state is closed, if both
userdata are still alive, the 2nd userdata will be collected before the
1st one, despite the reference.

-- Roberto

Reply | Threaded
Open this post in threaded view
|

Re: userdata environment garbage collection

Timothy Hunter

On Mar 25, 2011, at Mar 25 10:05 AM, Roberto Ierusalimschy wrote:

>> I allocate a userdata and then call lua_setfenv to set an environment
>> table for it. Then I allocate a 2nd userdata and store it in the
>> environment table. Eventually Lua collects the 1st userdata.
>>
>> I assume that the 2nd userdata will not be collected until the
>> reference in the environment table goes away. Is that correct?
>
> Mostly, but there is one detail: when the state is closed, if both
> userdata are still alive, the 2nd userdata will be collected before the
> 1st one, despite the reference.
>
> -- Roberto
>

Is the 2nd userdata collected first because it was allocated after the 1st one? Are userdata collected in reverse order of their allocation?