lua-users home
lua-l archive

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


Well, this pattern ought to work as long as you don't call anything L-related in your Cleanup(). You are likely to leak memory though, but it shouldn't crash. Generally though, C++ exceptions and non-local C jumps that Lua uses for coroutine management don't play well together.

-----Original Message-----
From: lua-bounces@bazar2.conectiva.com.br [mailto:lua-bounces@bazar2.conectiva.com.br] On Behalf Of Aidin Abedi
Sent: Thursday, October 02, 2008 2:05 PM
To: Lua list
Subject: Re: I need your help! C++ exception in coroutine Crashes.

Thank you for your replies.
I have a panic handler but its not called since I only use lua_pcall.
I use exception-handling outside and it seems like coroutine.resume disables it.
The code at its grittiest:

----------------------------------------
C++:

int main() {
        try {
                lua_pcall(L, ...);

        } catch (...) {
                Cleanup();
                exit(0);
        }

        return 0;
}

int ThrowCppException(lua_State *L) {
        throw "I'm a exception";
        return 0;
}

----------------------------------------
Lua:

print "main"
ThrowCppException() -- app catches exception and exits gracefully

function foo()
        print "coroutine"
        ThrowCppException() -- app crashes without catching exception
end

co = coroutine.create(foo)
coroutine.resume(co)
----------------------------------------


On Thu, Oct 2, 2008 at 9:57 PM, Bilyk, Alex <ABilyk@maxis.com> wrote:
> Do you handle the exception in the same function or someplace outside?
>
> -----Original Message-----
> From: lua-bounces@bazar2.conectiva.com.br [mailto:lua-bounces@bazar2.conectiva.com.br] On Behalf Of Patrick Donnelly
> Sent: Thursday, October 02, 2008 11:49 AM
> To: Lua list
> Subject: Re: I need your help! C++ exception in coroutine Crashes.
>
> On Thu, Oct 2, 2008 at 6:37 AM, Aidin Abedi <aidinabedi@gmail.com> wrote:
>> Hello
>>
>> I've embedded lua and when I call a c++ function (that throws a c++
>> exception) inside a coroutine my app crashes badly. Windows just says:
>>
>> "Runtime Error!
>> This application has requested the Runtime to terminate it in an unusual way.
>> Please contact the application's support team for more information."
>>
>> I do have a c++ exception-catch outside the pcall. There is no crash
>> if I call the same function (that throws a exception) from inside
>> lua's main-thread.
>>
>> This issue must have been detected before. Please help me!
>> I appreciate any ideas. I thank you for your time.
>>
>
> It sounds to me like you did a generic lua_call in the main thread
> (instead of lua_resume). When Lua catches the error, and no error
> handler was set for that thread, it will exit the main application.
> You should implement a panic handler to check if this is the case.
>
> Cheers,
>
> --
> -Patrick Donnelly
>
> "One of the lessons of history is that nothing is often a good thing
> to do and always a clever thing to say."
>
> -Will Durant
>