lua-users home
lua-l archive

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


>-----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