lua-users home
lua-l archive

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


Thanks Sam, that does make sense. 

Yeah I was talking about the second case: where my C code calls a lua
function (it doesn't do a return back to lua).  

So I guess that the definitive answer is that I should pop the return
values off the stack.

Thanks again guys

Beric Holt
http://buzzrick.true.geek.nz 

-----Original Message-----
From: lua-bounces@bazar2.conectiva.com.br
[mailto:lua-bounces@bazar2.conectiva.com.br] On Behalf Of Sam Roberts
Sent: Friday, 27 October 2006 1:29 p.m.
To: lua@bazar2.conectiva.com.br
Subject: Re: Pop Return results after pcall'ing

On Fri, Oct 27, 2006 at 12:29:56PM +1300, Beric Holt wrote:
> The reason that I ask is because I was implementing a Lua Function
caller helper (similar to http://www.lua.org/pil/25.3.html in PiL) and I
saw it mention that "because the function may return strings, call_va
cannot pop the results from the stack. It is up to the caller to pop
them, after it finishes using occasional string results (or after
copying them to other buffers)."

I think you are confusing two cases, when you do a lua_pcall() from
your C code, and when lua calls your C code.

When lua calls you, you get a stack. When your function returns, the
return values on the stack go back to the lua code that called you (or
maybe discarded, v = foo(); vs. foo(); ), but the entire stack is
cleaned up, nothing is left.

When you call lua functions on the lua_State with lua_call/lua_pcall,
those function's args go onto the stack, and the return values are on
the stack. If you don't pop the return values,  they will accumulate
there... unless you return to lua, see the above paragraph.

So, you can accumulate some stuff on your stack, then do a "return 2;"
from C to return the top 2 to lua, and everything else is GCed. But if
you don't return to lua, you could overflow the C stack you were given.

I don't know if that is clear, but hope it helps.

Sam