Repository navigation
fix: Destroy ncnn gpu instance before process exit - #104
deepin-bot[bot] merged 1 commit into
Conversation
|
CLA Assistant Lite bot All contributors have signed the CLA ✍️ ✅ |
Reviewer's GuideData structuresAdd static storage for the OCREngine singleton so shutdown can access and release the instance created by instance().
APIExpose shutdown() as the lifecycle API for releasing OCR and ncnn GPU resources before process exit.
Uses of the APICall OCREngine::shutdown() after each application event loop returns, before main() exits.
ImplementationResolve and invoke ncnn's GPU teardown symbol dynamically, releasing the OCR driver first, and update singleton creation to use the shared instance pointer.
ConfigurationAdd the missing SPDX copyright and license header to the dconfig installation CMake file.
Tips and commandsInteracting with Sourcery
Customizing Your ExperienceAccess your dashboard to:
Getting Help
|
There was a problem hiding this comment.
Hey - I've found 1 issue
Prompt for AI Agents
Please address the comments from this code review:
## Individual Comments
### Comment 1
<location path="src/engine/OCREngine.cpp" line_range="35" />
<code_context>
+ return;
+
+ // 先释放 OCR 驱动,其内部 ncnn Net/Mat 持有 GPU 资源,必须在 GPU 实例销毁前释放
+ delete s_instance->ocrDriver;
+ s_instance->ocrDriver = nullptr;
+
</code_context>
<issue_to_address>
**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`.
</issue_to_address>| return; | ||
|
|
||
| // 先释放 OCR 驱动,其内部 ncnn Net/Mat 持有 GPU 资源,必须在 GPU 实例销毁前释放 | ||
| delete s_instance->ocrDriver; |
There was a problem hiding this comment.
🟠 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`.6208b88 to
19697f3
Compare
Explicitly release the OCR driver and destroy the ncnn global Vulkan GPU instance before main() returns. Leaving it to libncnn's __cxa_finalize handler may run after the Vulkan ICD has been unloaded, so vkDestroyImageView() operates on an invalid VkDevice and crashes. fix: 进程退出时销毁 ncnn GPU 实例,修复关闭 OCR 窗口后的崩溃 在 main() 返回前先释放 OCR 驱动,再显式调用 ncnn::destroy_gpu_instance(), 避免 libncnn 在 __cxa_finalize 阶段使用已失效的 VkDevice 析构 GPU 资源。 chore: 补齐 install_dconfig.cmake 的 SPDX 版权头,修复 license-check。 Bug: https://pms.uniontech.com/bug-view-382595.html
19697f3 to
e6c3519
Compare
deepin pr auto review🤖 AI 代码审查报告📊 总体评价
🔍 详细分析1. 语法逻辑 ✅评价: 语法正确,逻辑清晰 ✅ 通过 潜在问题: 建议: ['在 shutdown() 末尾添加 2. 代码质量 ✅评价: 代码结构清晰,注释完整 ✅ 通过 潜在问题: 建议: ['可将 app->exec() + shutdown() + return 模式抽取为辅助函数,减少 main.cpp 中的代码重复'] 3. 代码性能 ✅评价: 性能良好,资源使用合理 ✅ 通过 潜在问题: 建议: [] 4. 代码安全 🔒评价: 存在0个安全漏洞 ✅ 通过
安全漏洞详情: 建议: [] 💡 改进建议代码示例// 建议在 OCREngine::shutdown() 末尾添加 s_instance 清理: // // void OCREngine::shutdown() // { // if (s_instance == nullptr) // return; // // delete s_instance->ocrDriver; // s_instance->ocrDriver = nullptr; // // 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"; // } // // // 新增:彻底清理单例对象 // delete s_instance; // s_instance = nullptr; // }本报告由 AI 代码审查工具自动生成 |
|
[APPROVALNOTIFIER] This PR is NOT APPROVED This pull-request has been approved by: max-lvs, tianming-1996 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 |
|
/merge |
问题
关闭 OCR 窗口后 deepin-ocr 进程退出阶段 SIGSEGV(PMS 382595 / WS-4159)。
coredump 调用栈(3 份 core 完全一致):
崩溃时传入的 VkDevice 句柄已不可访问(gdb 读取报 Cannot access memory)。ncnn 全局 Vulkan GPU 实例直到
__cxa_finalize阶段才析构,此时 Vulkan ICD 可能已先卸载,vkDestroyImageView()使用失效句柄导致崩溃。修改
OCREngine新增shutdown():先释放 OCR 驱动(其内部 ncnn Net/Mat 持有 GPU 资源),再显式调用ncnn::destroy_gpu_instance()。main()在app->exec()返回后调用OCREngine::shutdown(),保证 GPU 实例在 ICD 卸载前销毁。QLibrary解析 ncnn 导出符号,不新增 libncnn 的编译/链接依赖。验证
Bug: https://pms.uniontech.com/bug-view-382595.html
Summary by Sourcery
Ensure ncnn GPU resources are destroyed safely before application shutdown to prevent Vulkan handle use after the driver is unloaded.
Bug Fixes:
Enhancements:
Chores: