fix: Process crash caused by closing the application.#71
Merged
deepin-bot[bot] merged 1 commit intolinuxdeepin:masterfrom Sep 23, 2025
Merged
fix: Process crash caused by closing the application.#71deepin-bot[bot] merged 1 commit intolinuxdeepin:masterfrom
deepin-bot[bot] merged 1 commit intolinuxdeepin:masterfrom
Conversation
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 pr auto review这段代码展示了在析构函数中对工作线程的处理,我来分析一下它的优缺点和改进建议: 优点:
改进建议:
MainWidget::~MainWidget()
{
// 1. 设置线程结束标志
m_isEndThread = 0;
// 2. 安全停止并清理工作线程
if (m_loadImagethread) {
// 请求线程安全退出
m_loadImagethread->requestInterruption();
// 等待线程结束,设置超时时间
if (!m_loadImagethread->wait(3000)) {
qWarning() << "OCR thread did not finish within 3 seconds, forcing termination";
m_loadImagethread->terminate();
m_loadImagethread->deleteLater();
m_loadImagethread = nullptr;
}
// 正常退出情况下,线程对象已在槽函数中处理
}
}
改进后的代码示例: // 在头文件中定义
constexpr int THREAD_TIMEOUT_MS = 3000; // 线程超时时间(毫秒)
MainWidget::~MainWidget()
{
try {
// 1. 原子操作设置线程结束标志
m_isEndThread.store(0, std::memory_order_release);
// 2. 安全停止并清理工作线程
std::lock_guard<std::mutex> lock(m_threadMutex);
if (m_loadImagethread && m_loadImagethread->isRunning()) {
// 记录停止尝试时间
qDebug() << "Attempting to stop OCR thread at" << QDateTime::currentDateTime().toString();
// 请求线程安全退出
m_loadImagethread->requestInterruption();
// 等待线程结束
if (!m_loadImagethread->wait(THREAD_TIMEOUT_MS)) {
qWarning() << "OCR thread did not finish within" << THREAD_TIMEOUT_MS
<< "ms, forcing termination at"
<< QDateTime::currentDateTime().toString();
m_loadImagethread->terminate();
if (!m_loadImagethread->wait(1000)) { // 额外等待1秒确保终止
qCritical() << "Failed to terminate OCR thread";
}
m_loadImagethread->deleteLater();
m_loadImagethread = nullptr;
} else {
qDebug() << "OCR thread stopped successfully at"
<< QDateTime::currentDateTime().toString();
}
}
} catch (const std::exception& e) {
qCritical() << "Error in destructor:" << e.what();
}
}这些改进提高了代码的健壮性、可维护性和安全性,同时提供了更好的错误处理和日志记录。根据实际应用场景,可能还需要进一步调整和优化。 |
lzwind
approved these changes
Sep 23, 2025
|
[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. DetailsNeeds approval from an approver in each of these files:Approvers can indicate their approval by writing |
Contributor
Author
|
/merge |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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 (参考v20)