当调用luaL_error时,为什么不调用c++对象析构函数?

时间:2022-09-25 19:01:55

I have a piece of code like this

我有一段这样的代码。

class Test
{
public:
    Test() {printf(">>> Test()\n");}
    ~Test() {printf(">>> ~Test()\n");}
}

int myFunc(lua_State *L)
{
    Test t;
    luaL_error(L, "error");
    return 0;
}

I know when lua complied by c complier it use longjmp to raise an error. So, I compiled it use c++ compiler so that it use c++ exception to hand the errors and the destructor should be called even if an error is thrown. But my problem is that the object's destructor is not called.

我知道当lua遵从c编译器时,它使用longjmp来提高一个错误。因此,我编译它使用c++编译器,以便它使用c++异常来处理错误,即使抛出错误,也应该调用析构函数。但我的问题是,对象的析构函数没有调用。

However, the following code is working (the destructor is called)

但是,下面的代码正在工作(调用析构函数)

int myFunc(lua_State *L)
{
    Test t;
    throw(1) // just for testing
    return 0;
}

Why this happend? I'm sure the LUAI_THROW macro is interpreted as throw key word.

为什么这发生?我确信LUAI_THROW宏被解释为抛出关键字。

2 个解决方案

#1


1  

The function luaL_error() will call exit() which cancels the whole execution of your program! The desctructor is not called then because the scope where Test t is in does not end. You should use a different functionality to be able to recover from an error.

How do you call the error from lua? I think you need to do a protected call using lua_cpcall to go arround this exit on error feature!

函数luaL_error()将调用exit(),它将取消程序的整个执行。因为测试t所在的范围没有结束,所以不调用desctructor。您应该使用不同的功能来从错误中恢复。你怎么称呼lua的错误?我认为您需要使用lua_cpcall做一个受保护的调用,以便在错误特性上绕过这个出口!

#2


1  

The root cause is related to exception handling mode in visual c++ compiler. I use the lua function (such as luaL_error) with extern "C" modifier to prevent compiler from name-mangling. And the default exception handling mode is /EHsc which assume extern "C" function don't throw exception. So, the exception can't be catched. The solution is change /EHsc to /EHs.

在visual c++编译器中,根本原因与异常处理模式有关。我使用lua函数(如luaL_error)和外部的“C”修饰符来防止编译器被命名为-mangling。默认的异常处理模式是/EHsc,它假设外部的“C”函数不抛出异常。所以,例外是不能被抓住的。解决办法是改变/EHsc到/EHs。

For more information please refer to http://msdn.microsoft.com/en-us/library/1deeycx5.aspx.

有关更多信息,请参见http://msdn.microsoft.com/en-us/library/1deeycx5.aspx。

#1


1  

The function luaL_error() will call exit() which cancels the whole execution of your program! The desctructor is not called then because the scope where Test t is in does not end. You should use a different functionality to be able to recover from an error.

How do you call the error from lua? I think you need to do a protected call using lua_cpcall to go arround this exit on error feature!

函数luaL_error()将调用exit(),它将取消程序的整个执行。因为测试t所在的范围没有结束,所以不调用desctructor。您应该使用不同的功能来从错误中恢复。你怎么称呼lua的错误?我认为您需要使用lua_cpcall做一个受保护的调用,以便在错误特性上绕过这个出口!

#2


1  

The root cause is related to exception handling mode in visual c++ compiler. I use the lua function (such as luaL_error) with extern "C" modifier to prevent compiler from name-mangling. And the default exception handling mode is /EHsc which assume extern "C" function don't throw exception. So, the exception can't be catched. The solution is change /EHsc to /EHs.

在visual c++编译器中,根本原因与异常处理模式有关。我使用lua函数(如luaL_error)和外部的“C”修饰符来防止编译器被命名为-mangling。默认的异常处理模式是/EHsc,它假设外部的“C”函数不抛出异常。所以,例外是不能被抓住的。解决办法是改变/EHsc到/EHs。

For more information please refer to http://msdn.microsoft.com/en-us/library/1deeycx5.aspx.

有关更多信息,请参见http://msdn.microsoft.com/en-us/library/1deeycx5.aspx。