lua-users home
lua-l archive

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


Hi,

May be your crash is not related to lua coroutines at all. I can confirm the statement of Alex that mixing up C++ exceptions with C longjumps is very critical. If you did not change the lua defaults for Windows you end up throwing an C++ exception that is caught by Lua and resubmitted as a C longjump! Unix like OS will use _longjmp/_setjmp and will likely run without problems.

At our Enigma project (http://www.nongnu.org/enigma), we do not use coroutines, but do throw exceptions in the C++ engine which may pass several layers of Lua/C++ stack frames until caught. When switching the project from Lua 4.1 to Lua 5.1 we did run into sporadic crashes just on Windows. I finally solved this problem by redefinition of "LUAI_TRY" for Windows by adding to luaconf.h:

#if defined(__cplusplus) && defined(CXXLUA)
#undef LUAI_TRY
#define LUAI_TRY(L,c,a)	try { a } catch(lua_longjmp*) \
	{ if ((c)->status == 0) (c)->status = -1; }
#endif

#endif

If you like to check the related macros and changes, have look at our svn repository, revision 187. The problem had been discussed on our mailing list in June 2006 (http://lists.gnu.org/archive/html/enigma-devel/2006-06/threads.html).

Greets,

Ronald

Bilyk, Alex wrote:
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