Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions install_dconfig.cmake
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
# SPDX-FileCopyrightText: 2025-2026 UnionTech Software Technology Co., Ltd.
# SPDX-License-Identifier: GPL-3.0-or-later

# expect that all dconfigs of plugin is saved in ./assets/configs
function(INSTALL_DCONFIG CONFIG_NAME)
set(DConfigPath ${CMAKE_SOURCE_DIR}/assets/configs)
Expand Down
40 changes: 35 additions & 5 deletions src/engine/OCREngine.cpp
Original file line number Diff line number Diff line change
@@ -1,24 +1,54 @@
// SPDX-FileCopyrightText: 2023 UnionTech Software Technology Co., Ltd.
// SPDX-FileCopyrightText: 2023-2026 UnionTech Software Technology Co., Ltd.
//
// SPDX-License-Identifier: GPL-3.0-or-later

#include "OCREngine.h"
#include <DOcr>
#include <QProcess>

Check warning on line 7 in src/engine/OCREngine.cpp

View workflow job for this annotation

GitHub Actions / cppcheck

Include file: <QProcess> not found. Please note: Cppcheck does not need standard library headers to get proper results.
#include <QFileInfo>

Check warning on line 8 in src/engine/OCREngine.cpp

View workflow job for this annotation

GitHub Actions / cppcheck

Include file: <QFileInfo> not found. Please note: Cppcheck does not need standard library headers to get proper results.
#include <QDebug>

Check warning on line 9 in src/engine/OCREngine.cpp

View workflow job for this annotation

GitHub Actions / cppcheck

Include file: <QDebug> not found. Please note: Cppcheck does not need standard library headers to get proper results.
#include <QLibrary>

Check warning on line 10 in src/engine/OCREngine.cpp

View workflow job for this annotation

GitHub Actions / cppcheck

Include file: <QLibrary> not found. Please note: Cppcheck does not need standard library headers to get proper results.
#include <dconfigmanager.h>

Check warning on line 11 in src/engine/OCREngine.cpp

View workflow job for this annotation

GitHub Actions / cppcheck

Include file: <dconfigmanager.h> not found. Please note: Cppcheck does not need standard library headers to get proper results.

namespace {
// ncnn::destroy_gpu_instance() 的导出符号(声明见 ncnn/gpu.h)。
// 这里通过符号解析调用,避免为维护分支新增 libncnn 的编译/链接依赖。
constexpr char kNcnnDestroyGpuInstance[] = "_ZN4ncnn20destroy_gpu_instanceEv";
}

OCREngine *OCREngine::s_instance = nullptr;

OCREngine *OCREngine::instance()
{
static OCREngine *ocr_detail = nullptr;
if (ocr_detail == nullptr) {
ocr_detail = new OCREngine;
if (s_instance == nullptr) {
s_instance = new OCREngine;
}
return s_instance;
}

void OCREngine::shutdown()

Check warning on line 29 in src/engine/OCREngine.cpp

View workflow job for this annotation

GitHub Actions / cppcheck

The function 'shutdown' is never used.
{
if (s_instance == nullptr)
return;

// 先释放 OCR 驱动,其内部 ncnn Net/Mat 持有 GPU 资源,必须在 GPU 实例销毁前释放
delete s_instance->ocrDriver;

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟠 High · OCR cleanup races with recognition

When the window closes while OCR is still running after the three-second wait expires, shutdown() deletes s_instance->ocrDriver and destroys ncnn GPU state while the worker is still in getRecogitionResult() or analyze(), causing use-after-free and a crash during close.

Wait for the OCR worker to finish before deleting the driver and destroying the GPU instance.

Also at src/main.cpp:89.

Prompt for AI agents
In `src/engine/OCREngine.cpp` at line 35:

**OCR cleanup races with recognition**

When the window closes while OCR is still running after the three-second wait expires, `shutdown()` deletes `s_instance->ocrDriver` and destroys ncnn GPU state while the worker is still in `getRecogitionResult()` or `analyze()`, causing use-after-free and a crash during close.

Wait for the OCR worker to finish before deleting the driver and destroying the GPU instance.

Also at `src/main.cpp:89`.

s_instance->ocrDriver = nullptr;

// 在进程退出前显式销毁 ncnn 全局 Vulkan GPU 实例。若留到进程退出阶段,
// Vulkan ICD 可能已经先卸载,vkDestroyImageView() 会使用失效的 VkDevice 导致崩溃。
QLibrary ncnnLib(QStringLiteral("libncnn.so.1"));
using DestroyGpuInstance = void (*)();
auto destroyGpuInstance = reinterpret_cast<DestroyGpuInstance>(
ncnnLib.resolve(kNcnnDestroyGpuInstance));
if (destroyGpuInstance != nullptr) {
destroyGpuInstance();
} else {
qWarning() << "Failed to resolve ncnn::destroy_gpu_instance";
}
return ocr_detail;
}

OCREngine::OCREngine()

Check warning on line 51 in src/engine/OCREngine.cpp

View workflow job for this annotation

GitHub Actions / cppcheck

Member variable 'OCREngine::ocrSetting' is not initialized in the constructor. Member variables of native types, pointers, or references are left uninitialized when the class is instantiated. That may cause bugs or undefined behavior.
{
//初始化变量
m_isRunning = false;
Expand Down
7 changes: 6 additions & 1 deletion src/engine/OCREngine.h
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// SPDX-FileCopyrightText: 2023 UnionTech Software Technology Co., Ltd.
// SPDX-FileCopyrightText: 2023-2026 UnionTech Software Technology Co., Ltd.
//
// SPDX-License-Identifier: GPL-3.0-or-later

Expand All @@ -21,6 +21,9 @@ class OCREngine
public:
static OCREngine *instance();

// 进程退出前释放 OCR 驱动并销毁 ncnn 全局 Vulkan GPU 实例
static void shutdown();

bool isRunning() const
{
return m_isRunning;
Expand All @@ -40,6 +43,8 @@ class OCREngine
// 某些机型,使用GPU进行OCR识别,会导致OCR崩溃
bool isGpuEnable();

static OCREngine *s_instance;

Dtk::Ocr::DOcr *ocrDriver;
std::atomic_bool m_isRunning;
QSettings *ocrSetting;
Expand Down
11 changes: 8 additions & 3 deletions src/main.cpp
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
// SPDX-FileCopyrightText: 2022 UnionTech Software Technology Co., Ltd.
// SPDX-FileCopyrightText: 2022-2026 UnionTech Software Technology Co., Ltd.
//
// SPDX-License-Identifier: GPL-3.0-or-later

#include "ocrapplication.h"
#include "service/ocrinterface.h"
#include "service/dbusocr_adaptor.h"
#include "engine/OCREngine.h"

#include <DWidget>

Check warning on line 10 in src/main.cpp

View workflow job for this annotation

GitHub Actions / cppcheck

Include file: <DWidget> not found. Please note: Cppcheck does not need standard library headers to get proper results.
#include <DLog>

Check warning on line 11 in src/main.cpp

View workflow job for this annotation

GitHub Actions / cppcheck

Include file: <DLog> not found. Please note: Cppcheck does not need standard library headers to get proper results.
#include <DWindowManagerHelper>
#include <DWidgetUtil>
#include <DGuiApplicationHelper>
Expand Down Expand Up @@ -67,7 +68,9 @@
if (cmdParser.isSet(dbusOption)) {
// 第一调用已 --dbus参数启动
qDebug() << "dbus register waiting!";
return app->exec();
int ret = app->exec();
OCREngine::shutdown();
return ret;
}

instance.openFile(QString(argv[1]));
Expand All @@ -82,7 +85,9 @@
return 0;
}

return app->exec();
int ret = app->exec();
OCREngine::shutdown();
return ret;
}


Loading