Skip to content

Conversation

@yixinshark
Copy link
Contributor

@yixinshark yixinshark commented Jul 8, 2025

as title

Log: as title
Pms: BUG-318023

Summary by Sourcery

Ensure proper Qt object ownership and cleanup to eliminate memory leaks in dde-session by parenting dynamically allocated objects and disposing of FIFO instances after use.

Bug Fixes:

  • Prevent memory leaks by assigning QApplication as parent to QDBusServiceWatcher, Session, WMSwitcher, and Fifo objects and deleting FIFO instances after use.

Enhancements:

  • Convert Fifo to a QObject subclass with parent support and integrate deleteLater on read completion.
  • Simplify FIFO write logic by executing it synchronously instead of spawning a separate thread.
  • Parent all dynamically allocated Qt objects to the application to leverage Qt’s automatic memory management.

@sourcery-ai
Copy link

sourcery-ai bot commented Jul 8, 2025

Reviewer's Guide

This PR attaches heap-allocated Qt objects to the application’s parent, refactors the Fifo class for QObject ownership, and adds explicit cleanup to prevent leaks.

Class diagram for updated Fifo ownership and construction

classDiagram
    class Fifo {
        +Fifo(QObject *parent = nullptr)
        +virtual ~Fifo()
        +int Read(QString &data)
        +int Write(QString data)
        -int m_fd
        -QString m_fifoPath
    }
    Fifo --|> QObject
Loading

Class diagram for updated Session and WMSwitcher construction

classDiagram
    class Session {
        +Session(QObject *parent = nullptr)
    }
    Session --|> QObject
    class WMSwitcher {
        +WMSwitcher(QObject *parent = nullptr)
    }
    WMSwitcher --|> QObject
Loading

File-Level Changes

Change Details Files
Attach session-related objects to QCoreApplication for automatic deletion
  • Passed &app as parent to QDBusServiceWatcher
  • Constructed Session and WMSwitcher with &app parent
src/dde-session/main.cpp
Scope and parent Fifo writer instance to avoid unmanaged allocation
  • Moved Fifo writer into a local block
  • Passed &app as parent when instantiating Fifo for writing
src/dde-session/main.cpp
Ensure reader-side Fifo is deleted after use
  • Added fifo->deleteLater() at end of read thread
src/dde-session/main.cpp
Refactor Fifo class to support QObject ownership
  • Inherit Fifo from QObject and add Q_OBJECT macro
  • Change constructor to accept QObject* parent and add virtual destructor
src/utils/fifo.h
src/utils/fifo.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

Copy link

@sourcery-ai sourcery-ai bot left a comment

Choose a reason for hiding this comment

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

Hey @yixinshark - I've reviewed your changes - here's some feedback:

  • You removed the background thread for the write‐FIFO and now invoke OpenWrite/Write on the main thread—this can block your UI, so consider keeping it in a worker thread or making it nonblocking.
  • Calling deleteLater() on the FIFO inside a QThreadPool task won’t free it if there’s no running event loop—either give it a QObject parent or delete it explicitly when done.
  • Make sure Fifo’s destructor properly closes m_fd and cleans up the FIFO file to avoid resource leaks at teardown.
Prompt for AI Agents
Please address the comments from this code review:
## Overall Comments
- You removed the background thread for the write‐FIFO and now invoke OpenWrite/Write on the main thread—this can block your UI, so consider keeping it in a worker thread or making it nonblocking.
- Calling deleteLater() on the FIFO inside a QThreadPool task won’t free it if there’s no running event loop—either give it a QObject parent or delete it explicitly when done.
- Make sure Fifo’s destructor properly closes m_fd and cleans up the FIFO file to avoid resource leaks at teardown.

## Individual Comments

### Comment 1
<location> `src/dde-session/main.cpp:103` </location>
<code_context>
-            qInfo()<<"leader pipe thread id: " << QThread::currentThreadId() << ", pid: " << curPid;
-            Fifo *fifo = new Fifo;
+
+        {   // none block
+            pid_t curPid = getpid();
+            Fifo *fifo = new Fifo(&app);
             fifo->OpenWrite();
             fifo->Write(QString::number(curPid));
-        });
-
+        }
         // We started the unit, open <dbus> and sleep forever.
         return app.exec();
</code_context>

<issue_to_address>
Switching from QThreadPool to direct execution for Fifo write may affect responsiveness.

Since Fifo writes are now synchronous, any delays or blocking in Fifo::OpenWrite or Fifo::Write could negatively impact startup or UI responsiveness. Please confirm this change is intentional and safe across all environments.
</issue_to_address>

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.

Comment on lines 103 to 108
{ // none block
pid_t curPid = getpid();
Fifo *fifo = new Fifo(&app);
fifo->OpenWrite();
fifo->Write(QString::number(curPid));
});

}
Copy link

Choose a reason for hiding this comment

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

issue (performance): Switching from QThreadPool to direct execution for Fifo write may affect responsiveness.

Since Fifo writes are now synchronous, any delays or blocking in Fifo::OpenWrite or Fifo::Write could negatively impact startup or UI responsiveness. Please confirm this change is intentional and safe across all environments.

as title

Log: as title
Pms: BUG-318023
@deepin-ci-robot
Copy link

deepin pr auto review

关键摘要:

  • main.cpp中,Fifo对象在多个线程中创建和销毁,可能会导致资源泄露或竞争条件。
  • Fifo类的构造函数中缺少对m_fifoPath的初始化检查,可能导致后续操作失败。
  • Fifo类缺少对文件描述符的异常处理,如果打开失败,应该有相应的错误处理机制。
  • Fifo类应该使用QScopedPointerstd::unique_ptr来管理m_fd,以避免手动管理内存的问题。
  • Fifo类应该提供异常安全的方法,例如在打开或读取/写入操作失败时抛出异常。
  • main.cpp中的QDBusServiceWatcherQThreadPool::globalInstance()->start的lambda表达式捕获了app对象,但没有检查app对象是否为空,可能会导致空指针解引用。

是否建议立即修改:

@deepin-ci-robot
Copy link

[APPROVALNOTIFIER] This PR is NOT APPROVED

This pull-request has been approved by: 18202781743, yixinshark

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

@yixinshark yixinshark merged commit 8284dc4 into linuxdeepin:master Jul 9, 2025
15 of 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