[Date Prev][Date Next][Thread Prev][Thread Next]
- Subject: Re: Error Handling and Resource Cleanup
- From: "D Burgess" <db@...>
- Date: Sat, 19 Jun 2004 2:11:45 +1000
There is info on this on the Wiki and previous message threads.
With VC6 if a C module throws an exception then the C++ does
Note that if you are using VC6 you must compile with
this allows C++ to believe that C functions might throw an
Whatever compiler you are using - check the exception handling
options for mixed C and C++.
Given the above, auto_ptrs work fine.
Francisco Socal <firstname.lastname@example.org> wrote:
>How can one be sure that locally allocated memory will be released when an error
>occurs during the execution of some function of the Lua API? As in the following
>code, per example: `foo` is never deleted as an error occurs and the execution
>is tranfered back to `main`.
> int pmain( lua_State* L )
> Foo* foo = new Foo;
> lua_pushstring( L, "This should be a table." );
> lua_pushstring( L, "The next line will cause a run-time error." );
> lua_gettable( L, -2 );
> delete foo;
> return 0;
> int main()
> lua_State* L = lua_open();
> lua_cpcall( L, pmain, 0 );
> return 0;
>Looking at the Lua exception handling mechanism, I noticed that it is possible
>to redefine `L_THROW` and `L_TRY` via the `LUA_USEEXCEPTIONS` macro, so as to
>use exceptions and let compiler generate the necessary cleanup code. This way,
>one could safely wrap `foo` with something like `std::auto_ptr` and just don't
>worry about memory management anynore.
>But I definitively don't want to change my Lua libraries and start using C++ to
>build them. So I was wondering how could that be done purely in Lua, possibly
>pushing `foo` somehow to the Lua stack as userdata and using its `__gc`
>metamethod... Any idea?