[Date Prev][Date Next][Thread Prev][Thread Next]
[Date Index]
[Thread Index]
- Subject: RE: Pop Return results after pcall'ing
- From: "Beric Holt" <beric@...>
- Date: Fri, 27 Oct 2006 12:29:56 +1300
Thanks Jerome
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 realised that I never pop returned variables off the stack after a function call. I do correctly handle strings: if I wish to store them beyond the immediate scope of the function call then I copy it into a new string something like this:
char *szLuaString = lua_tostring(luaVM, 1);
char *szNewString = new char[strlen(szLuaString) + 1];
strcpy_s(m_szNewString , strlen(szLuaString) , szLuaString);
So anyway, are you saying that I don't need to pop the return values off the stack; lua will deal with that. Would anyone say that it is still good practice to pop the returned values after I've used them? Or should I just not bother?
Sorry for being a little bit dense. I just want to make sure that I'm getting it right.
Cheers
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 Jerome Vuarand
Sent: Friday, 27 October 2006 11:38 a.m.
To: Lua list
Subject: RE: Pop Return results after pcall'ing
All return values are discarded after being used. There is no such problem with the stack size, you don't have to manage anything in your C functions.
And on the other hand you cannot use the stack as a storage place for your objects. If you want them to be preserved after the call to your C function you have to put them in some table.
-----Message d'origine-----
De : lua-bounces@bazar2.conectiva.com.br [mailto:lua-bounces@bazar2.conectiva.com.br] De la part de Beric Holt
Envoyé : 26 octobre 2006 18:06
À : Lua list
Objet : Pop Return results after pcall'ing
I've previously asked about whether function parameters are automatically pop'ed off the stack after the function call
(http://lua-users.org/lists/lua-l/2006-03/msg00074.html) but what happens if you don't pop the returned values after you have finished with them. I assume that the stack will simple grow indefinitely until you run out of memory?
Cheers
Beric Holt
http://buzzrick.true.geek.nz