[Date Prev][Date Next][Thread Prev][Thread Next]
[Date Index]
[Thread Index]
- Subject: lua_newthread returning 2 values
- From: "Erik Cassel" <erik@...>
- Date: Mon, 20 Oct 2008 11:12:14 -0700
Occasionally lua_newthread is returning two values - rather than one - as it
is supposed to do!
As expected, the top of the stack points to a thread, but there is a second
value, which is a userdata object. I?ve tracked the problem down as far as
the garbage collector:
GCTM()
singlestep()
luaC_step()
lua_newthread()
When we enter GCTM the top of the stack is 0. Now GCTM calls luaD_precall,
which calls a C __gc metamethod for one of my user data objects. Afterwards,
luaD_poscall *sometimes* fails to restore the stack size properly. For some
reason L->base doesn?t point to where it did before calling luaD_precall.
The end result is a stack size of 1 rather than 0 after returning from
luaD_precall.
I?ve traced the code many times. Usually luaD_poscall restores the stack
properly and lua_newthread returns a single item. Also, I?ve confirmed that
my custom __gc metamethod does not push anything onto the stack. It always
returns 0.
I?m at a loss on how to proceed. The code in luaD_precall/luaD_poscall is
pretty hard to read, so I haven?t figured out how to dig any deeper than I
have.
I?ve wondered if any side effects of my __gc metamethod could be the
problem. The cleanup code in the __gc metamethod might possibly re-enter Lua
somewhere, but I haven?t found evidence of this, yet.
It?s going to be very hard to make a reproducible sample code for this one.
The Lua library is embedded inside a large production-level game.
Nonetheless, I?ll try to simplify the framework as much as possible if
nothing else turns up. I?m not suggesting that there?s a bug in Lua ? I?ve
probably broken something somewhere. The problem is I can?t figure out
where!
Any suggestions on what might be happening?
-Erik