Skip to content

Conversation

@lichaofan2008
Copy link
Contributor

When performing the destruction operation, it is necessary to wait for the child thread to exit to avoid errors in resource release.

fix: 关闭应用引起进程崩溃

析构操作时,需要等待子线程退出,避免资源释放出错。

Bug: https://pms.uniontech.com/bug-view-334545.html

When performing the destruction operation, it is necessary to wait for the child thread to exit to avoid errors in resource release.

fix: 关闭应用引起进程崩溃

析构操作时,需要等待子线程退出,避免资源释放出错。

Bug: https://pms.uniontech.com/bug-view-334545.html
@deepin-ci-robot
Copy link

deepin pr auto review

这段代码是关于线程管理的改进,我来分析一下它的优点和可以改进的地方:

优点:

  1. 添加了线程安全退出机制,使用 requestInterruption() 而不是直接 terminate()
  2. 设置了合理的超时时间(3秒)
  3. 在线程结束后正确清理资源
  4. 使用 lambda 表达式简化了 finished 信号的处理

改进建议:

  1. 线程状态检查

    • 在请求中断前,应该先检查线程是否已经在运行
    • 建议添加 if (m_loadImagethread && m_loadImagethread->isRunning()) 的判断
  2. 错误处理

    • 当线程超时被强制终止时,应该记录更详细的错误信息
    • 建议添加 qCritical() 而不是 qWarning() 来标记严重问题
  3. 资源管理

    • 在线程对象被删除前,确保没有其他地方还在使用这个线程
    • 建议使用 QSharedPointer 或 QScopedPointer 来管理线程对象的生命周期
  4. 代码风格

    • lambda 表达式中使用了 [this] 捕获,这可能会增加对象的生命周期,需要确保不会造成循环引用
    • 建议将线程管理相关的代码封装到一个单独的函数中,提高代码可读性
  5. 性能优化

    • 3秒的超时时间可能需要根据实际任务复杂度进行调整
    • 考虑使用 QThreadPool 和 QRunnable 来替代直接的线程管理,这样可以更好地利用系统资源
  6. 安全性

    • 在多线程环境下访问共享资源时,应该添加适当的互斥锁保护
    • 确保所有对线程对象的访问都是线程安全的

改进后的代码建议:

MainWidget::~MainWidget()
{
    // 程序即将结束,线程标志结束
    m_isEndThread = 0;
    
    // 等待线程结束
    if (m_loadImagethread && m_loadImagethread->isRunning()) {
        m_loadImagethread->requestInterruption();
        
        if (!m_loadImagethread->wait(3000)) {
            qCritical() << "OCR thread failed to stop gracefully, terminating forcefully";
            m_loadImagethread->terminate();
            m_loadImagethread->wait(); // 确保线程完全终止
        }
    }
    
    // 确保线程对象被正确清理
    if (m_loadImagethread) {
        m_loadImagethread->deleteLater();
        m_loadImagethread = nullptr;
    }
    
    if (m_mainGridLayout && m_buttonHorizontalLayout) {
        m_mainGridLayout->removeItem(m_buttonHorizontalLayout);
    }
}

void MainWidget::openImage(const QImage &img, const QString &name)
{
    // ... 其他代码 ...
    
    connect(m_loadImagethread, &QThread::finished, [this]() {
        QMutexLocker locker(&m_threadMutex); // 确保线程安全
        if (m_loadImagethread) {
            m_loadImagethread->deleteLater();
            m_loadImagethread = nullptr;
        }
    });
    
    m_loadImagethread->start();
}

这些改进可以提高代码的健壮性、安全性和可维护性。

@deepin-ci-robot
Copy link

[APPROVALNOTIFIER] This PR is NOT APPROVED

This pull-request has been approved by: lichaofan2008, lzwind

The full list of commands accepted by this bot can be found here.

Details Needs approval from an approver in each of these files:

Approvers can indicate their approval by writing /approve in a comment
Approvers can cancel approval by writing /approve cancel in a comment

@lichaofan2008
Copy link
Contributor Author

/forcemerge

@deepin-bot
Copy link
Contributor

deepin-bot bot commented Sep 23, 2025

This pr force merged! (status: unstable)

@deepin-bot deepin-bot bot merged commit 35ba262 into linuxdeepin:release/1050U2 Sep 23, 2025
4 of 5 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants