diff --git a/src/mainwidget.cpp b/src/mainwidget.cpp index 483c900a..12501166 100644 --- a/src/mainwidget.cpp +++ b/src/mainwidget.cpp @@ -52,19 +52,22 @@ MainWidget::MainWidget(QWidget *parent) : MainWidget::~MainWidget() { - //程序即将结束,线程标志结束 - m_isEndThread = 0; - // 等待线程结束 - if (m_loadImagethread) { - m_loadImagethread->requestInterruption(); // 请求线程中断 - if (!m_loadImagethread->wait(3000)) { // 等待线程结束,超时时间为3秒 - qCWarning(dmOcr) << "OCR thread TIMEOUT"; - m_loadImagethread->terminate(); // 如果线程没有结束,强制终止线程 - m_loadImagethread->deleteLater(); // 删除线程对象 - m_loadImagethread = nullptr; - } - // 正常结束的情况下delete操作在槽函数中执行 + m_isEndThread.store(false); + + QThread *thread = m_loadImagethread; + m_loadImagethread = nullptr; + if (!thread) + return; + + thread->requestInterruption(); + if (!thread->wait(3000)) { + qCWarning(dmOcr) << "OCR thread TIMEOUT"; + thread->terminate(); + // terminate() is asynchronous; the object must not be destroyed while + // the worker can still access MainWidget and the OCR engine. + thread->wait(); } + delete thread; } void MainWidget::setupUi(QWidget *Widget) @@ -349,10 +352,6 @@ void MainWidget::setupConnect() connect(this, &MainWidget::sigResult, this, [ = ](const OcrResult &result) { applyOcrResult(result); deleteLoadingUi(); - if(m_needReRunRec) { - m_needReRunRec = false; - runRec(false); - } }); } @@ -495,8 +494,8 @@ void MainWidget::openImage(const QImage &img, const QString &name) void MainWidget::runRec(bool needSetImage) { - if(m_loadImagethread && m_loadImagethread->isRunning()) { - m_needReRunRec = true; + if (m_loadImagethread) { + m_needReRunRec.store(true); return; } @@ -509,20 +508,26 @@ void MainWidget::runRec(bool needSetImage) if(needSetImage) { OCREngine::instance()->setImage(*m_currentImg); } - if (!m_loadImagethread) { - m_loadImagethread = QThread::create([ = ]() { - const OcrResult result = OCREngine::instance()->getRecognitionResult(); - //判断程序是否退出 - if (1 == m_isEndThread) { - emit sigResult(result); - } - }); - } - connect(m_loadImagethread, &QThread::finished, [this](){ - m_loadImagethread->deleteLater(); - m_loadImagethread = nullptr; + QThread *thread = QThread::create([this]() { + const OcrResult result = OCREngine::instance()->getRecognitionResult(); + if (m_isEndThread.load()) { + emit sigResult(result); + } }); - m_loadImagethread->start(); + m_loadImagethread = thread; + + // Always marshal cleanup back to MainWidget's thread. The previous + // context-less connection executed in the OCR thread and could access a + // null or dangling m_loadImagethread while the widget was being destroyed. + connect(thread, &QThread::finished, this, [this, thread]() { + if (m_loadImagethread == thread) + m_loadImagethread = nullptr; + thread->deleteLater(); + + if (m_needReRunRec.exchange(false)) + runRec(false); + }, Qt::QueuedConnection); + thread->start(); } void MainWidget::loadHtml(const QString &html) diff --git a/src/mainwidget.h b/src/mainwidget.h index b6ce96c3..485aff12 100644 --- a/src/mainwidget.h +++ b/src/mainwidget.h @@ -17,6 +17,8 @@ #include #include +#include + #include "resulttextview.h" #include "textloadwidget.h" @@ -113,7 +115,7 @@ private slots: QWidget *m_emptyWidget; - int m_isEndThread = 1; + std::atomic_bool m_isEndThread{true}; QSettings *ocrSetting; std::atomic_bool m_needReRunRec = false;