lua-users home
lua-l archive

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


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;

    v.push_back( 1 );

    lua_pushstring( L, "Leaked some memory" );
    lua_error( L );

    return 0;
}

The vector's destructor never gets called and its memory is leaked.

-Kevin

> I was wondering whether it's problematic to use 'error' 
> function as an exception mechanism? The language shootout had 
> an entry for this and, apparently, both Lua versions used 
> 'error' function as a model for exceptions.  I have explored 
> the mailing list archive and have some idea of what to 
> expect. But if you are toying with the same idea why not discuss it.
> 
> 1. Are there any common platforms that would have hard time 
> with setjmp/longjmp C functions and thus with Lua error 
> function in general. How about game consoles? PDAs? 2. Does 
> calling 'error' impair garbage collection in any way? That 
> is, if I had some user data referenced in a function that 
> calls error would such user data be collected properly? 3. If 
> you have used 'error' for exceptions, what are you 
> impressions? What worked, what didn't?
> 
> Thanks,
> AB
>