lua-users home
lua-l archive

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


I saw an earlier post along with a patch where the poster replaced the
setjmp()/longjmp() idiom with C++ exceptions.

It worked for him, but there are size and performance penalties with C++
exceptions.  The only alternative (that I know) is to take extra care
with your C++ code.

I have a patch and a set of macros that allow you to do something like
this:

luaL_try( L )
{
    //protected code
}
luaL_catch( 2 )
{
    //handle exception
}
luaL_tryend;

This opens a new can of worms.  Anything declared outside the try block
and used inside the try block must be declared volatile.  This is
typical of the the setjmp()/longjmp() exception idiom.

I eventually opted for not using this method.

-Kevin

-----Original Message-----
From: lua-bounces@bazar2.conectiva.com.br
[mailto:lua-bounces@bazar2.conectiva.com.br] On Behalf Of Bilyk, Alex
Sent: Thursday, December 18, 2003 11:40 AM
To: Lua list
Subject: RE: error, setjmp and exceptions


>-----Original Message-----
>From: Joshua Jensen [mailto:jjensen@workspacewhiz.com]
>Sent: Thursday, December 18, 2003 11:28 AM
>To: 'Lua list'
>Subject: RE: error, setjmp and exceptions
>
>
>> This may be common knowledge, but I thought I'd mention it.
>>
>> Calling lua_error() from a C/C++ function can result in
>> leaked resources.  This can be more subtle in C++ than in C.
>>
>> int func( lua_State* L )
>> {
>>     std::vector< int > v;
>>     ...
>>     lua_error( L );
>>     return 0;
>> }
>>
>> The vector's destructor never gets called and its memory is leaked.
>
>I've run into this SO many times.  I'm not sure if it is
>doable, but perhaps
>lua_error() should set a flag for the error condition and only
>fire when the
>Lua VM is back in control?

But there is no guaranty the C function calling an lua_error is the only
one that needs cleaning. So, I don't think the flag is quite a solution
to abandoned resources on C stack. It would be much easier to say istead
int func( lua_State* L )
{
    {
        std::vector< int > v;
        ...
    }

    lua_error( L );
    return 0;
}

but then, as you pointed out, one is prone to not having done so and
would not clean any other C functions anyway. I wonder if one could use
a C++ exception instead of lua_error to accomplish similar thing. 

Alex