Skip to content

fix: 释放语音服务插件资源#694

Merged
deepin-bot[bot] merged 1 commit into
linuxdeepin:release/eaglefrom
Resurgamz:release/eagle
Jun 17, 2026
Merged

fix: 释放语音服务插件资源#694
deepin-bot[bot] merged 1 commit into
linuxdeepin:release/eaglefrom
Resurgamz:release/eagle

Conversation

@Resurgamz

@Resurgamz Resurgamz commented Jun 17, 2026

Copy link
Copy Markdown

Log: 修复MusicVoiceService未释放VoicePlugin的问题
Task: https://pms.uniontech.com/task-view-390625.html
Influence: 服务释放时通过Qt父子对象机制回收VoicePlugin,并释放内部

Summary by Sourcery

Ensure VoicePlugin instances are properly parented and release their internal resources on destruction to prevent leaks.

Bug Fixes:

  • Fix VoicePlugin not being deleted with MusicVoiceService by setting MusicVoiceService as its QObject parent.
  • Release VoicePlugin settings configuration object in the VoicePlugin destructor to avoid memory leaks.

@sourcery-ai sourcery-ai Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Hey - I've left some high level feedback:

  • Consider making m_settings a child QObject (e.g., by passing this to fromJsonFile or calling m_settings->setParent(this)) so that you can rely on Qt’s parent-child lifetime management instead of manually deleting the pointer in the destructor.
  • Once m_voice is parented to MusicVoiceService, ensure there are no other manual deletions or ownership transfers of m_voice elsewhere in the codebase to avoid double-free or dangling pointer issues.
Prompt for AI Agents
Please address the comments from this code review:

## Overall Comments
- Consider making `m_settings` a child `QObject` (e.g., by passing `this` to `fromJsonFile` or calling `m_settings->setParent(this)`) so that you can rely on Qt’s parent-child lifetime management instead of manually deleting the pointer in the destructor.
- Once `m_voice` is parented to `MusicVoiceService`, ensure there are no other manual deletions or ownership transfers of `m_voice` elsewhere in the codebase to avoid double-free or dangling pointer issues.

Sourcery is free for open source - if you like our reviews please consider sharing them ✨
Help me be more useful! Please click 👍 or 👎 on each comment and I'll use the feedback to improve your reviews.

  Log: 修复MusicVoiceService未释放VoicePlugin的问题
  Task: https://pms.uniontech.com/task-view-390625.html
  Influence: 服务释放时通过Qt父子对象机制回收VoicePlugin,并释放内部
@deepin-ci-robot

Copy link
Copy Markdown

deepin pr auto review

★ 总体评分:100分

■ 【总体评价】

代码完美修复了Qt对象树未挂载导致的内存泄漏问题,符合Qt内存管理最佳实践
逻辑严密且引入了必要的空指针防御机制,无任何扣分项

■ 【详细分析】

  • 1.语法逻辑(完全正确)✓

修复前MusicVoiceServicenew VoicePlugin()未指定父对象,VoicePluginDSettings::fromJsonFile返回值未做判空且未挂载对象树,导致生命周期结束后无法自动释放。修复后通过传入this和调用setParent(this)构建了完整的级联释放链路,并在voiceplugin.cpp中增加了对m_settings的空指针检查,逻辑严密无懈可击。

  • 2.代码质量(优秀)✓

代码遵循了Qt框架的核心设计模式(对象树机制),通过极简的代码改动解决了资源管理问题。增加的if (m_settings)判空语句体现了良好的防御性编程习惯,有效避免了因资源文件缺失或解析失败导致的潜在崩溃风险。

  • 3.代码性能(高效)✓

setParent操作在Qt底层仅涉及内部指针的重新赋值以及对象列表的插入,时间复杂度为O(1),不涉及深拷贝或复杂的系统调用,对运行时性能零损耗。

  • 4.代码安全(存在0个安全漏洞)✓

漏洞对比统计:新增漏洞 0 个,减少漏洞 0 个,持平 0 个
本次代码修改纯粹是对内存生命周期管理的标准修复,未引入任何命令注入、越界访问、权限绕过等安全风险,且通过空指针检查增强了程序的健壮性,整体代码安全可靠。

  • 建议:保持当前的Qt对象树管理规范,后续新增QObject派生类时均应显式传递parent指针。

■ 【改进建议代码示例】

diff --git a/src/libmusic-plugin/musicvoiceservice.cpp b/src/libmusic-plugin/musicvoiceservice.cpp
index 7c6754f40..67344af1e 100644
--- a/src/libmusic-plugin/musicvoiceservice.cpp
+++ b/src/libmusic-plugin/musicvoiceservice.cpp
@@ -8,7 +8,7 @@
 
 MusicVoiceService::MusicVoiceService()
 {
-    m_voice = new VoicePlugin();
+    m_voice = new VoicePlugin(this);
 }
 
diff --git a/src/libmusic-plugin/voiceplugin.cpp b/src/libmusic-plugin/voiceplugin.cpp
index 81de7f234..58a59ac7c 100644
--- a/src/libmusic-plugin/voiceplugin.cpp
+++ b/src/libmusic-plugin/voiceplugin.cpp
@@ -21,6 +21,9 @@
 VoicePlugin::VoicePlugin(QObject *parent): QObject(parent)
 {
     m_settings = Dtk::Core::DSettings::fromJsonFile(":/speech/data/deepin-music-speechconfig.json");
+    if (m_settings) {
+        m_settings->setParent(this);
+    }
 }

@deepin-ci-robot

Copy link
Copy Markdown

[APPROVALNOTIFIER] This PR is NOT APPROVED

This pull-request has been approved by: max-lvs, Resurgamz

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

@Resurgamz

Copy link
Copy Markdown
Author

/merge

@deepin-bot deepin-bot Bot merged commit bb9eb3c into linuxdeepin:release/eagle Jun 17, 2026
16 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