Unsolved Exception is not catched in QThread
-
Hi,
I'm trying to use the exiv2 library to modify image metadata. This library uses C++ exceptions. My code is working in the main thread ( e.g. in the constructor of the QThread ), but when I move it to the threads run() method I the exception does not get caught and the program crashes:try { Exiv2::ImageFactory::open("C:\\Path_to\\invalid_file.tiff"); } catch ( ... ) { qDebug("got it!"); }
I'm using the MSVC2015 and Qt 5.9.1
-
Hi! Just tested it on desktop Linux. Created a widgets projects, added a pushbutton to the mainwindow. And created a worker class for the thread:
worker.h#ifndef WORKER_H #define WORKER_H #include <QObject> class Worker : public QObject { Q_OBJECT public: Worker(QObject *parent = nullptr); ~Worker(); public slots: void process(); signals: void finished(); }; #endif // WORKER_H
worker.cpp
#include "worker.h" #include <unistd.h> #include <exiv2/exiv2.hpp> #include <QDebug> Worker::Worker(QObject *parent) : QObject(parent) { } Worker::~Worker() { } void Worker::process() { try { Exiv2::ImageFactory::open("C:\\Path_to\\invalid_file.tiff"); } catch (...) { qDebug() << "meow"; } emit finished(); }
in mainwindow.cpp
void MainWindow::on_pushButton_clicked() { ui->pushButton->setEnabled(false); // ignore this auto t = new QThread; auto w = new Worker; w->moveToThread(t); connect(t, &QThread::started, w, &Worker::process); connect(w, &Worker::finished, t, &QThread::quit); connect(w, &Worker::finished, w, &Worker::deleteLater); connect(t, &QThread::finished, t, &QThread::deleteLater); connect(t, &QThread::finished, this, &MainWindow::enableButton); t->start(); }
So far, works as expected. Do you do anything else with Exiv besides that in your code / in another thread? Maybe some part of the exiv library isn't thread-safe.
-
Hi Wieland,
I've just tested your code on Windows/MSVC2015 and it crashed. I've also tested my code on Linux and there it works.
From the beginning I would have been glad to develop under Linux, but I rely on some Windows hardware driver.
One Question regarding your code: You included unistd.h . Is this for purpose or some leftover?
Have you some more idea what could be the reason?