From 2322e6225500a7a16b38376c1205459c10cea125 Mon Sep 17 00:00:00 2001 From: dengzhongyuan Date: Wed, 2 Dec 2026 15:18:28 +0800 Subject: [PATCH] fix: prevent OCR thread cleanup crash MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 1. Marshal QThread finished cleanup back to MainWidget's thread. 2. Prevent reuse of a thread before its cleanup has completed. 3. Wait for a terminated OCR thread before deleting its object. Log: Prevent OCR worker thread cleanup crashes. Influence: 1. Close the OCR window while recognition is running and verify no crash. 2. Trigger repeated recognition and verify each OCR thread exits cleanly. 3. Repeat the screenshot-to-OCR flow on SW64 and x86. fix: 修复 OCR 识别线程清理竞态 1. 将 QThread finished 清理显式投递回 MainWidget 所在线程。 2. 避免在线程清理完成前复用同一个线程对象。 3. 终止 OCR 线程后等待其真正退出再删除对象。 Log: 修复 OCR 识别线程退出时的清理崩溃。 Influence: 1. OCR 识别过程中关闭窗口,验证不会崩溃。 2. 重复触发识别,验证每次 OCR 线程均可正常退出。 3. 在 SW64 和 x86 上重复截图进入 OCR 的流程。 Log: Prevent OCR worker thread cleanup crashes. Issue: V-9975 multica: V-9975 --- src/mainwidget.cpp | 67 +++++++++++++++++++++++++--------------------- src/mainwidget.h | 4 ++- 2 files changed, 39 insertions(+), 32 deletions(-) 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;