在另一个线程中调用 luaL_error 会抛出 qWarning

Calling luaL_error in another thread throws qWarning

在与 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 错误?感谢您的宝贵时间。

luaL_error 的文档指出:

This function never returns, ...

在其他地方,lua 错误处理文档有:

If an error happens outside any protected environment, Lua calls a panic function (see lua_atpanic) and then calls abort, thus exiting the host application. Your panic function can avoid this exit by never returning (e.g., doing a long jump to your own recovery point outside Lua).

因此您可以将 luaL_error(和 lua_error)视为引发致命错误。它们对于保护模式下的脚本是致命的,或者对于保护模式外的进程是致命的。

如果您的 C 代码是从脚本调用的,您可以使用 lua_pcall 在保护模式下启动脚本。这是通常的情况。如果您的 C 代码不是从脚本调用的,那么您可能不想使用 luaL_error,因为它的主要优点是报告有关脚本中发生错误的位置的信息。

您还可以通过 luaL_where 获取有关错误位置的信息,并通过其他方式将其报告给用户。