lua-users home
lua-l archive

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


If you compile Lua as C, then it'll use setjmp/longjmp, and you don't get stack unwinding.
If you compile Lua as C++, then it'll use exceptions, and you do get stack unwinding.

On Fri, Dec 21, 2012 at 2:23 PM, Glenn Schmottlach <gschmottlach@gmail.com> wrote:
Something has always bothered me with using C++ to write Lua modules. As I understand it, the call to lua_error() is implemented using C's setjmp/longjmp routines which make no effort to do any necessary "stack-unwinding" which is where C++ does things like calling the destructors for local (stack) based objects. So if I have such local objects in my function and call lua_error() will the destructors get called for these objects? My suspicion is that they will not get invoked unless Lua is doing something sneaky in it's lua_error() implementation. Does anyone know the details of this behavior?


 

On Fri, Dec 21, 2012 at 9:15 AM, Philipp Kraus <philipp.kraus@flashpixx.de> wrote:

Am 21.12.2012 um 14:42 schrieb Roberto Ierusalimschy:

>>> Or better yet, call lua_error in the first place (instead of throwing
>>> a C++ exception). In Lua, you can catch such errors with 'pcall'.
>>
>>
>> Can I catch this error on the Lua script? [...]
>
>>> ................. In Lua, you can catch such errors with 'pcall'.
>                      ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

Sorry, I have confused with lua_pcall (the C fuction). So I just
run a lua function with

if pcall(myfunc()) then
   no error
else
   error
end

A try-catch around a block does not work, so I must call each "function"
with pcall, do I ?

Thanks

Phil