在与GUI分开的线程中运行的lua状态下调用luaL_error()
时,将打印警告QObject::~QObject: Timers cannot be stopped from another thread
并关闭应用程序。
经过大量测试,我能够在一个紧凑的示例程序中模拟我当前的工作流程来重现此内容。下面是代码:
mainwindow.h
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include <QMainWindow>
#include <QDebug>
#include <QThread>
#include "lua_src/lua.hpp"
class Worker : public QObject
{
Q_OBJECT
public:
Worker() : QObject(){}
public slots:
void process()
{
lua_State *L = luaL_newstate();
luaL_dostring(L, "x=5");
luaL_error(L, "test error");
lua_close(L);
emit finished();
}
signals:
void finished();
};
namespace Ui {
class MainWindow;
}
class MainWindow : public QMainWindow
{
Q_OBJECT
public:
explicit MainWindow(QWidget *parent = 0);
~MainWindow();
Worker *worker;
QThread *workerThread;
private:
Ui::MainWindow *ui;
};
#endif // MAINWINDOW_H
mainwindow.cpp
#include "mainwindow.h"
#include "ui_mainwindow.h"
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
worker = new Worker();
workerThread = new QThread();
worker->moveToThread(workerThread);
connect(workerThread, SIGNAL(started()), worker, SLOT(process()));
connect(worker, SIGNAL(finished()), workerThread, SLOT(quit()));
connect(worker, SIGNAL(finished()), worker, SLOT(deleteLater()));
connect(workerThread, SIGNAL(finished()), workerThread, SLOT(deleteLater()));
workerThread->start();
}
MainWindow::~MainWindow()
{
delete ui;
}
main.cpp
#include "mainwindow.h"
#include <QApplication>
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
MainWindow w;
w.show();
return a.exec();
}
我本以为工作线程进程将继续并且lua状态将简单地关闭。但是,这使lua错误处理无用。我已经为该项目提供了Lua 5.3的源代码。
我确实注意到的一件事是,此问题似乎只发生在GUI应用程序上,而不发生在控制台应用程序上。
有人知道这里发生了什么以及如何从QThread内部处理lua错误吗?感谢您的宝贵时间。
答案 0 :(得分:1)
luaL_error
的文档说明:
此函数永不返回,...
在其他地方,lua错误处理文档具有:
如果在任何受保护的环境之外发生错误,Lua会调用panic函数(请参见lua_atpanic),然后调用abort,从而退出主机应用程序。您的恐慌功能可以避免再也不返回(例如,在Lua之外跳到您自己的恢复点),从而避免退出。
因此,您可以将luaL_error
(和lua_error
)视为致命错误。它们对于处于保护模式下的脚本是致命的,或者对于处于保护模式之外的进程是致命的。
如果从脚本中调用C代码,则可以使用lua_pcall
以保护模式启动脚本。这是通常的情况。如果未从脚本中调用C代码,则您可能不希望使用luaL_error
,因为它的主要优点是报告有关脚本中错误发生位置的信息。
您还可以使用luaL_where
获取有关错误位置的信息,并以其他方式将其报告给用户。