H
H
hydra_132016-05-31 09:20:55
Qt
hydra_13, 2016-05-31 09:20:55

How to catch exceptions in QThread?

It is required to catch system exceptions outside the main thread. In the main thread, everything is very simple:
Redefine QApplication::notify() and rejoice:

bool notify(QObject *obj, QEvent *ev)
{
    bool result = false;
    try{
        result = QApplication::notify(obj, ev);
    } catch(...){
        qDebug() << "exception";
        result = false;
    }
    return result;
}

As a test, I used the following code (click event on QPushButton):
void MainWindow::on_pb1_clicked()
{
    QObject* obj = (QObject*) 0x00112233;
    delete obj;
}

Everything works fine when specifying the compilation option -EHa. But there is one problem. When using QThread, this construction does not work. Example:
Redefined on_pb1_clicked() function:
void MainWindow::on_pb1_clicked()
{
QPluginLoader pluginLoader("d:/Sources/qt/_BUILDS/Plugin/Desktop_Qt_5_5_1_MSVC2013_64bit/release/FirstPlugin.dll");
    QObject *pluginObj = pluginLoader.instance();
    if (pluginObj){
        CensorPluginRootInstance* root = qobject_cast<CensorPluginRootInstance *>(pluginObj);
        if (root) {
            PluginInterface* plugin = root->createPluginObject();
            if (plugin){
                ui->teStat->append("plugin load ok!");
                QThread* thread = new QThread;
                connect(thread, &QThread::started, plugin, &PluginInterface::Work);
                plugin->moveToThread(thread);
                thread->start();
            } else
                ui->teStat->append("plugin not loaded 3!");
        }else
            ui->teStat->append("plugin not loaded 2!");
    }else
        ui->teStat->append("plugin not loaded 1!");
}

Plugin code:
bool Plugin::Work()
{
    char* pc = (char*)0x00112233;
    delete pc;
    return false;
}

The plugin is loaded, as evidenced by the inscription "plugin load ok!", and then:
f76d61c04cdc4a3ebbcd2d46e9415b0e.png
How to programmatically catch exceptions thrown from a non-main thread?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
#
#algooptimize #bottize, 2016-05-31
@hydra_13

Pass your function, inside it already call the plugin with interception,
Movetothread, what is it?
Still there is a debugger, symbols.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question