Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Exception is not catched in QThread
Forum Updated to NodeBB v4.3 + New Features

Exception is not catched in QThread

Scheduled Pinned Locked Moved Unsolved General and Desktop
3 Posts 2 Posters 1.9k Views 2 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • Q Offline
    Q Offline
    qmmj
    wrote on 15 Aug 2017, 17:09 last edited by
    #1

    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

    1 Reply Last reply
    0
    • ? Offline
      ? Offline
      A Former User
      wrote on 15 Aug 2017, 20:00 last edited by
      #2

      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.

      1 Reply Last reply
      3
      • Q Offline
        Q Offline
        qmmj
        wrote on 16 Aug 2017, 11:15 last edited by qmmj
        #3

        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?

        1 Reply Last reply
        0

        1/3

        15 Aug 2017, 17:09

        • Login

        • Login or register to search.
        1 out of 3
        • First post
          1/3
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • Users
        • Groups
        • Search
        • Get Qt Extensions
        • Unsolved