Skip to content

fix: fix memory leak in moveText when from equals to - #598

Merged
pengfeixx merged 1 commit into
linuxdeepin:masterfrom
pengfeixx:agent/pms-bug-bot/30d7fdaeb486
Sep 20, 2026
Merged

pengfeixx merged 1 commit into
linuxdeepin:masterfrom
pengfeixx:agent/pms-bug-bot/30d7fdaeb486

Conversation

@pengfeixx

@pengfeixx pengfeixx commented Sep 18, 2026

Copy link
Copy Markdown
Contributor

fix: fix memory leak in moveText when from equals to

  1. Root cause: TextEdit::moveText() missing else branch for from == to,
    heap-allocated UndoList, DragInsertTextUndoCommand and
    DeleteBackCommand objects never get freed
  2. Fix: add else branch to delete list, insertCommand and delCommand
    (if non-null) when from == to, no behavior change for other paths
  3. Impact: only affects drag-drop text to same position scenario, no
    regression risk for normal move operations

Influence:

  1. Test drag selected text to the same position (from == to)
  2. Test drag text forward (from < to) with undo
  3. Test drag text backward (from > to) with undo

fix: 修复moveText函数from等于to时的内存泄漏

  1. 根因:TextEdit::moveText() 缺少 from == to 的 else 分支,
    堆分配的 UndoList、DragInsertTextUndoCommand 和
    DeleteBackCommand 对象未被释放导致内存泄漏
  2. 方案:添加 else 分支,在 from == to 时释放 list、
    insertCommand 及 delCommand(非空时),不影响其他路径
  3. 影响:仅影响拖拽文本到原位置的场景,对正常移动操作无回归风险

Influence:

  1. 测试拖拽选中文本到原位置(from == to)
  2. 测试向前拖拽文本(from < to)并撤销
  3. 测试向后拖拽文本(from > to)并撤销

PMS: BUG-185

Summary by Sourcery

Fix memory leaks in text moves where the source and destination positions are identical.

Bug Fixes:

  • Prevent memory leaks when moving text to the same position by releasing unused undo command objects.
  • Preserve existing text movement and undo behavior for forward and backward moves.

1. Root cause: TextEdit::moveText() missing else branch for from == to,
   heap-allocated UndoList, DragInsertTextUndoCommand and
   DeleteBackCommand objects never get freed
2. Fix: add else branch to delete list, insertCommand and delCommand
   (if non-null) when from == to, no behavior change for other paths
3. Impact: only affects drag-drop text to same position scenario, no
   regression risk for normal move operations

Influence:
1. Test drag selected text to the same position (from == to)
2. Test drag text forward (from < to) with undo
3. Test drag text backward (from > to) with undo

fix: 修复moveText函数from等于to时的内存泄漏

1. 根因:TextEdit::moveText() 缺少 from == to 的 else 分支,
   堆分配的 UndoList、DragInsertTextUndoCommand 和
   DeleteBackCommand 对象未被释放导致内存泄漏
2. 方案:添加 else 分支,在 from == to 时释放 list、
   insertCommand 及 delCommand(非空时),不影响其他路径
3. 影响:仅影响拖拽文本到原位置的场景,对正常移动操作无回归风险

Influence:
1. 测试拖拽选中文本到原位置(from == to)
2. 测试向前拖拽文本(from < to)并撤销
3. 测试向后拖拽文本(from > to)并撤销

PMS: BUG-185

@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.

Sorry @pengfeixx, you've used your own review budget of 250,000 diff characters for the last 7 days.

You can request another review in 3 days and 16 hours by commenting @sourcery-ai review. Upgrade to get a review now.

@sourcery-ai

sourcery-ai Bot commented Sep 18, 2026

Copy link
Copy Markdown
Reviewer's guide (collapsed on small PRs)

Reviewer's Guide

The PR fixes a memory leak in TextEdit::moveText() by cleaning up temporary undo objects when dragged text is moved to its original position, while preserving normal forward and backward move behavior.

Flow diagram for moveText cleanup when source equals destination

flowchart TD
    A["TextEdit::moveText(from, to, text, copy)"] --> B{from equals to}
    B -->|No| C["appendCom(insertCommand)"]
    C --> D["m_pUndoStack->push(list)"]
    B -->|Yes| E["delete list"]
    E --> F["delete insertCommand"]
    F --> G{delCommand non-null}
    G -->|Yes| H["delete delCommand"]
    G -->|No| I["Cleanup complete"]
    H --> I
Loading

File-Level Changes

Change Details Files
Fix the same-position drag path to release temporary undo objects instead of leaking them.
  • Add an explicit from == to cleanup branch.
  • Delete the heap-allocated undo list and insert command, plus the delete command when present.
  • Leave the existing undo-stack behavior unchanged for moves where from != to.
src/editor/dtextedit.cpp

Tips and commands

Interacting with Sourcery

  • Trigger a new review: Comment @sourcery-ai review on the pull request.
  • Continue discussions: Reply directly to Sourcery's review comments.
  • Generate a GitHub issue from a review comment: Ask Sourcery to create an
    issue from a review comment by replying to it. You can also reply to a
    review comment with @sourcery-ai issue to create an issue from it.
  • Generate a pull request title: Write @sourcery-ai anywhere in the pull
    request title to generate a title at any time. You can also comment
    @sourcery-ai title on the pull request to (re-)generate the title at any time.
  • Generate a pull request summary: Write @sourcery-ai summary anywhere in
    the pull request body to generate a PR summary at any time exactly where you
    want it. You can also comment @sourcery-ai summary on the pull request to
    (re-)generate the summary at any time.
  • Generate reviewer's guide: Comment @sourcery-ai guide on the pull
    request to (re-)generate the reviewer's guide at any time.
  • Resolve all Sourcery comments: Comment @sourcery-ai resolve on the
    pull request to resolve all Sourcery comments. Useful if you've already
    addressed all the comments and don't want to see them anymore.
  • Dismiss all Sourcery reviews: Comment @sourcery-ai dismiss on the pull
    request to dismiss all existing Sourcery reviews. Especially useful if you
    want to start fresh with a new review - don't forget to comment
    @sourcery-ai review to trigger a new review!

Customizing Your Experience

Access your dashboard to:

  • Enable or disable review features such as the Sourcery-generated pull request
    summary, the reviewer's guide, and others.
  • Change the review language.
  • Add, remove or edit custom review instructions.
  • Adjust other review settings.

Getting Help

@deepin-ci-robot

Copy link
Copy Markdown

deepin pr auto review

🤖 AI 代码审查报告

总体评分: 99 分 (通过阈值: 70分)

Pass


📊 总体评价

项目 结果
审查结论 代码审查通过
评分详情 总体评分 99 分,大于 70 分通过阈值,代码质量符合要求。本次变更正确修复了 moveText 函数中 from == to 时的内存泄漏问题,代码逻辑清晰,无安全漏洞引入。

🔍 详细分析

1. 语法逻辑 ✅

评价: 优秀 ✅ 通过

潜在问题:
✅ 未发现明显问题

建议: 语法正确,逻辑清晰。else 分支正确处理了 from == to 的情况,delete list、delete insertCommand 操作正确——这些对象通过 new 在堆上分配但从未被添加到撤销栈。if (delCommand) 空指针检查正确,因为 delCommand 仅在 !copy 时分配。无双重释放风险:在 from == to 路径中,命令对象从未被 appendCom 到 list 中。


2. 代码质量 ✅

评价: 优秀 ✅ 通过

潜在问题:

  1. src/editor/dtextedit.cpp:3838 - else 分支缺少 qDebug 调试日志,与 if(from<to) 和 else if(from>to) 分支的调试日志风格不一致

建议: 代码结构清晰,注释完整。建议在 else 分支中添加 qDebug() << "Moving text with from == to (no-op, cleanup)"; 以保持与其他分支的调试日志一致性。


3. 代码性能 ✅

评价: 优秀 ✅ 通过

潜在问题:
✅ 未发现明显问题

建议: 性能良好,资源使用合理。本次修复消除了 from == to 场景下的内存泄漏,堆分配的 UndoList、DragInsertTextUndoCommand 和 DeleteBackCommand 对象现在被正确释放。无不必要的操作。


4. 代码安全 🔒

评价: 优秀 ✅ 通过

🔐 发现 0 个安全漏洞

安全漏洞详情:
✅ 未发现安全漏洞

建议: 存在0个安全漏洞。本次变更未引入任何安全漏洞,修复内存泄漏提升了代码的内存安全性。


💡 改进建议代码示例

// 建议在 else 分支添加调试日志以保持一致性
} else {
    qDebug() << "Moving text with from == to (no-op, cleanup)";
    delete list;
    delete insertCommand;
    if (delCommand) {
        delete delCommand;
    }
}

本报告由 AI 代码审查工具自动生成

@deepin-ci-robot

Copy link
Copy Markdown

[APPROVALNOTIFIER] This PR is NOT APPROVED

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

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

@pengfeixx
pengfeixx merged commit f25a638 into linuxdeepin:master Sep 20, 2026
17 checks passed
@pengfeixx
pengfeixx deleted the agent/pms-bug-bot/30d7fdaeb486 branch September 20, 2026 03:09
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