lua-users home
lua-l archive

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


----- Original Message ----- 
From: "Steve Heller" <steve@steveheller.com>
To: "Lua list" <lua@bazar2.conectiva.com.br>
Sent: Tuesday, September 12, 2006 9:40 AM
Subject: Re: Problem with table created from C


On Mon, 11 Sep 2006 23:57:03 +0200, "Jose Luis Hidalgo"
<joseluis.hidalgo@gmail.com> wrote:

>Hi Steve,
>
>    Just one question, Why lua_settop(m_L, 0) after each function?

So I don't leave data on the stack accidentally. I had stack overflows
before I put that in there.

It will empty all the data in the stack, so what you have push in the stack 
will be gone, include your table.


>Does the SetThreadLuaPointer function create a thread? 

No, it records the Lua environment pointer in C thread local storage.

>I'm not sure if
>doing a lua_settop to 0 will not destroy data that you expect to be
>there after that... When I need to ensure that a function leaves the
>stack as it was before it execution I always do that:
>
>// this function is for internal use, as you can see it can not be called from
>// lua, is a helper function called by other C functions.
>void myFunction(lua_State *L, int param1, int param2, ...) {
>      int top = lua_gettop(L); // get the current top
>     /*
>        here do thins with Lua as you want...
>        top var helps to access data too :)
>      */
>     lua_settop(L,top); //restore the stack
>}
>
>
>May be you can check what kind of data do you have in the stack before
>doing a settop to 0. Remember that Threads for example are pushed onto
>the stack as other values, so if you remove them without store them in
>registry or a table, or whatever... they will be collected.
>
>Hope it helps, :)

I'll let you know whether it does. Thanks!

Steve