lua-users home
lua-l archive

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


Yes, I understand that lua_pushstring() has no effect here. Although the second value was discarded, isn't sOldTitle just referencing the memory location of the returned value of the function SetTitle() ? That is what I was attempting to describe in the first message.

here's the C function (extremely simple, inside all the armor). It just calls another function that sets the new value and returns the previous one:

int LuaSetTitle( struct lua_State* L )
       if ( lua_gettop( L ) == 1 )
           char str[_MAX_PATH];
strncpy( str, g_LuaScript.SetTitle( lua_tostring( L, 1 ) ), sizeof(str) );
           lua_pushstring( L, str );
           return 1;
   lua_pushstring( L, "Default" );
   return 1;

Michael Newberry

----- Original Message ----- From: "Alex Sandro Queiroz e Silva" <>
To: "Lua list" <>
Sent: Wednesday, February 02, 2005 12:20 PM
Subject: Re: Q on functions as first class values


Michael Newberry wrote:

I think I see what is happening (is this correct?): I have assigned sOldTitle as the value of SetTitle(), so the fact that I did not update sOldTitle by the second call to SetTitle() means that sOldTitle was automatically updated by lua_pushstring() inside SetTitle(). Is this correct? If so, then I could solve the problem by using a dummy value to catch the unused return from the second call to SetTitle(). But I wonder if there is a better way; otherwise that is a "gotcha" waiting to happen again.

lua_pushstring just pushes a string on the stack, it's the assigment that binds that string to a particular variable (sOldTitle in this case). The result in the second call was probably discarded. It'd help a lot if we could see the code of SetTitle().