Understanding lua_ref and lua_unref

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

Understanding lua_ref and lua_unref

Raymond Jacobs
Greetings all,

in my test code:

while(1)
{
        lua_pushstring(state,"woot!");

        int ref=lua_ref(state,1);

        lua_unref(state,ref);

        lua_gc(state,LUA_GCCOLLECT,0);
}

I observe ref is returned like this

iteration 1  ref is 1
iteration 2  ref is 1
iteration 3  ref is 2
further iterations increase ref by 1


I assumed that the above code would continually return the same reference id, since i was referencing and releasing the object
which means the id should again be free for use, it seemed to do this on iterations 1 and 2 but on iterations 3 and above the reference count
continues to grow.

can anyone clear up what I might be doing wrong?
or perhaps my assumptions about lua_ref and lua_unref are wrong?

-Raymond Jacobs
Owner
Ethereal Darkness Interactive


Reply | Threaded
Open this post in threaded view
|

Re: Understanding lua_ref and lua_unref

Greg Falcon
On 4/13/06, Raymond Jacobs <[hidden email]> wrote:

> Greetings all,
>
>  in my test code:
>
>  while(1)
>  {
>          lua_pushstring(state,"woot!");
>          int ref=lua_ref(state,1);
>          lua_unref(state,ref);
>          lua_gc(state,LUA_GCCOLLECT,0);
>  }
>
>  I observe ref is returned like this
>
>  iteration 1  ref is 1
>  iteration 2  ref is 1
>  iteration 3  ref is 2
>  further iterations increase ref by 1

I tried to reproduce your problem in Lua 5.1, but could not do so.  I
also looked at the Lua code in question and couldn't figure out what
could cause this.

What version of Lua are you using?  More importantly, how are you
inspecting the return values of lua_ref()?  Are you stepping through
this code in a debugger, or is there more to your program than you
pasted here?

Note that the lua_ref compatibility macro simply calls luaL_ref, using
the registry as the reference table.  If you use integer keys in the
registry for your own purposes, this will conflict with lua_ref().

Greg F
Reply | Threaded
Open this post in threaded view
|

Re: Understanding lua_ref and lua_unref

Raymond Jacobs
Hi Greg,
 
thanks for the reply =)
 
I am using Lua 5.1, and I am compiling it using MSVS 8, C++
 
I am running this test code right after i call lua_open, to avoid any other factors.
 
I am stepping through the code using the debugger, and getting the reference values
I described.
 
i stepped into the luaL_ref and related functions but I am not very 'up' on the concepts of how it is supposed to work, from what I see it seems that when a ref is created it is put into a table and that index is returned, when somthing is un-refed the index it occupied is put into a 'free list' which the ref checks first to reuse old ref id's, given the functionality it seems to do that for two iterations (returns 1 then returns 1 again) but after that it simply starts to accumulate.
 
do you think this could be some weird compiler specific problem?
 
-Raymond

 
On 4/14/06, Greg Falcon <[hidden email]> wrote:
On 4/13/06, Raymond Jacobs <[hidden email]> wrote:

> Greetings all,
>
>  in my test code:
>
>  while(1)
>  {
>          lua_pushstring(state,"woot!");
>          int ref=lua_ref(state,1);
>          lua_unref(state,ref);
>          lua_gc(state,LUA_GCCOLLECT,0);
>  }
>
>  I observe ref is returned like this
>
>  iteration 1  ref is 1
>  iteration 2  ref is 1
>  iteration 3  ref is 2
>  further iterations increase ref by 1

I tried to reproduce your problem in Lua 5.1, but could not do so.  I
also looked at the Lua code in question and couldn't figure out what
could cause this.

What version of Lua are you using?  More importantly, how are you
inspecting the return values of lua_ref()?  Are you stepping through
this code in a debugger, or is there more to your program than you
pasted here?

Note that the lua_ref compatibility macro simply calls luaL_ref, using
the registry as the reference table.  If you use integer keys in the
registry for your own purposes, this will conflict with lua_ref().

Greg F

Reply | Threaded
Open this post in threaded view
|

Re: Understanding lua_ref and lua_unref

Roberto Ierusalimschy
> I am using Lua 5.1, and I am compiling it using MSVS 8, C++
> [...]
> do you think this could be some weird compiler specific problem?

Are you using DirectX in your program?

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

Re: Understanding lua_ref and lua_unref

Raymond Jacobs
Roberto,

absolutely, and before i saw this message i happened upon another thread with issues in directX.

i turned on FPU preserve, and bingo, works fine.

this REALLY should be better documented since it would have been very easy for me to never find the reason for this issue.

and given that Lua is used in games frequently the occurrence of these issues must be high =/

on a side note, good work on Lua, we are looking forward to having it drive our new game engine for our next game =)

We are an independent game developer and our first game was Morning's Wrath (www.morningswrath.com),

our next game is called Malathedra (www.malathedra.com) and will be using Lua, so you can add it to your list of
applications using Lua if you wish =)

thanks,

Raymond Jacobs
Owner,
Ethereal Darkness Interactive
www.edigames.com

On 4/17/06, Roberto Ierusalimschy <[hidden email]> wrote:
> I am using Lua 5.1, and I am compiling it using MSVS 8, C++
> [...]
> do you think this could be some weird compiler specific problem?

Are you using DirectX in your program?

-- Roberto

Reply | Threaded
Open this post in threaded view
|

Re: Understanding lua_ref and lua_unref

Roberto Ierusalimschy
> this REALLY should be better documented since it would have been very easy
> for me to never find the reason for this issue.

It is in the bug page in the site, and we will soon release 5.1.1 (a
bug-fix release) that should avoid this problem.

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

Re: Understanding lua_ref and lua_unref

Raymond Jacobs
Roberto,
 
I remember browsing the bug list a while back, and that one must have slipped me =/
 
I realize it's not a problem with lua, but I can't imagine how many people have tried to use
DX and Lua together and eventually ruled Lua as being 'buggy' and dropped it. =/
 
 
On a side note, I don't belive the lua documentation makes mention that co-routines created in
C must be manualy removed from the stack when done. this problem also threw me for a gigantic loop,
which took days to figure out. It does mention they are 'subject to garbage collection'
but I didin't make the connection being a new Lua user. I wonder if that should be noted they need to be removed to be collected?
 
Anyhow, I'll be sure to keep a closer eye on the documentation, thanks for your help.
 
On 4/18/06, Roberto Ierusalimschy <[hidden email]> wrote:
> this REALLY should be better documented since it would have been very easy
> for me to never find the reason for this issue.

It is in the bug page in the site, and we will soon release 5.1.1 (a
bug-fix release) that should avoid this problem.

-- Roberto

Reply | Threaded
Open this post in threaded view
|

Re: Understanding lua_ref and lua_unref

Roberto Ierusalimschy
> I remember browsing the bug list a while back, and that one must have
> slipped me =/

Probably it was not there yet :)

-- Roberto