lua-users home
lua-l archive

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


Hi, and thanks for your prompt reply (also goes to Grisha)!

On Wed, Jun 16, 2004 at 08:42:20AM +0000 lua@ricilake.net wrote:
> > Hi there,
> >
> > I am currently experiencing unpleasant segfaults when recursively
> > traversing Lua tables. This is the Lua function whose return value I'd
> > like to traverse:
> >
> 
> >     void walk (lua_State *L, int idx) {
> >         lua_pushnil(L);
> >
> >         printf("idx=%i\n", idx);
> 
> In the recursion, at this point, idx is -1. After the lua_pushnil,
> stack element -1 (the top of the stack is nil). The subsequent call
> to lua_next(L, idx) attempts to do a "next" operation on nil.

Hmmh, ok. I understand that this is not a good thing to have. I played
around a bit with the recursive call to walk() and it looks as though I
have to do:

    case LUA_TTABLE:
	walk(L, lua_gettop(L));
	break;

and, oh joy, it works!

> The Lua API does not protect you from type errors like this unless
> you enable API checking (see the Lua FAQ on the Wiki "Why does my
> program segfault?" for a pointer on how to enable this.)

Ah, yes, very nice. I'll recompile Lua according to this advice, also
adding debugging symbols which would have been quite helpful for me in
the past few days. It's indeed so that Lua seems to produce more segfaults 
than other interpreters I know.

> My suggestion is that you use absolute (base-relative) instead of
> negative (top-relative) stack indexes.

I assume that the above call to lua_gettop() will switch things into
base-relative addressing (idx is 3 on the second invocation of 'walk'). 
I find this easier to understand.

Thanks again!
Tassilo
-- 
$_=q#",}])!JAPH!qq(tsuJ[{@"tnirp}3..0}_$;//::niam/s~=)]3[))_$-3(rellac(=_$({
pam{rekcahbus})(rekcah{lrePbus})(lreP{rehtonabus})!JAPH!qq(rehtona{tsuJbus#;
$_=reverse,s+(?<=sub).+q#q!'"qq.\t$&."'!#+sexisexiixesixeseg;y~\n~~dddd;eval