[Date Prev][Date Next][Thread Prev][Thread Next]
- Subject: Re: Error handling in C++
- From: David Jones <drj@...>
- Date: Wed, 25 Jun 2003 12:56:25 +0000
In message <20030625120610.GA10486@malva.ua>, Max Ischenko writes:
> I've read the Lua manual, but still not sure about the following code:
> int errcode = luaL_loadfile(L, filename);
> if (errcode > 0)
> const char* msg = lua_tostring(L, -1);
> lua_pop(L, 1);
> throw LuaError(msg);
> Does Lua guarantee that LuaError would be instantiated with valid msg
> string or I should copy it before doing lua_pop?
You should make a copy. Or use the registry.
Specifically, lua only guarantees that the
pointer returned by lua_tostring is valid whilst the corresponding lua
value remains on the stack. As soon as the lua_pop is executed in the
code above Lua is at liberty to invalidate the msg pointer (by garbage
collecting the string for example, but possibly in future releases
merely by moving it).
This is explained in section 3.5 of the Lua 5.0 Reference Manual where
it explains lua_tostring.