介绍
MSVC 2017 64 位,Win 10 64 位,Qt Creator
我有一个类CameraControl
建立与相机的连接并且是QMainWindow
类的数据成员:
class RaGaCCMainView : public QMainWindow
{
Q_OBJECT
...
public:
explicit RaGaCCMainView(QWidget *parent = nullptr);
~RaGaCCMainView();
private:
CameraControl cameraControl;
QThread cameraWorkerThread;
...
...
};
class CameraControl : public QObject
{
Q_OBJECT
...
public:
explicit CameraControl(QObject *parent = nullptr);
~CameraControl();
void stopImageAcquisition();
...
...
};
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
RaGaCCMainView w;
w.setAttribute(Qt::WA_QuitOnClose);
w.show();
return a.exec();
}
CameraControl 被移动到cameraWorkerThread
:
RaGaCCMainView::RaGaCCMainView(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::RaGaCCMainView)
{
...
this->cameraControl.moveToThread(&(this->cameraWorkerThread));
// Object should be deletable after the thread finished!
this->connect(&this->cameraWorkerThread, &QThread::finished, &this->cameraControl, &QObject::deleteLater);
... // some connects
this->cameraWorkerThread.start();
}
在以下cameraWorkerThread
的析构函数中被停止并终止RaGaCCMainView
:
RaGaCCMainView::~RaGaCCMainView()
{
this->cameraWorkerThread.quit();
this->cameraWorkerThread.terminate();
delete this->ui;
}
如您所见,如果用户关闭主视图Qt::WA_QuitOnClose
(当用户决定关闭主窗口时,为了以一种很好的方式结束相机连接,我调用了一个函数stopImageAcquisition
来处理一些相机内容(停止获取图像)并可能引发异常:
CameraControl::~CameraControl()
{
this->stopImageAcquisition(); // Here an exception could/will be thrown!
USB1_3M_CloseCamera(this->cameraHandle);
USB1_3M_TerminateLibrary();
}
问题
我认为这不会有任何问题,因为在析构函数中抛出的东西无论如何都会被忽略。
但是,当我关闭主窗口w
并引发异常时,我abort() has been called
从 msvc 收到一条消息:
当然,我不知道这里有什么问题。我建议这个问题与工作线程的处理有关......
我希望异常被忽略它应该是什么(或者这也是对本意行为的误解?)
问题
- 是什么导致了这个问题?
- 我是否需要使用
try
或者catch
是否有其他可行的解决方案?
我总是感谢你的帮助。