Skip to content

fix: set parent for NetItem to prevent JS release#526

Merged
18202781743 merged 1 commit intolinuxdeepin:masterfrom
18202781743:master
Mar 24, 2026
Merged

fix: set parent for NetItem to prevent JS release#526
18202781743 merged 1 commit intolinuxdeepin:masterfrom
18202781743:master

Conversation

@18202781743
Copy link
Contributor

@18202781743 18202781743 commented Mar 24, 2026

  1. Added setParent() calls for root and delete items in
    NetManagerPrivate constructor
  2. Added setParent() call when adding child items in
    NetItemPrivate::addChild()
  3. These changes ensure proper parent-child relationships are
    established
  4. Prevents potential issues where JavaScript could release NetItem
    objects prematurely

Influence:

  1. Test network item hierarchy remains stable during operations
  2. Verify parent-child relationships are correctly maintained
  3. Test JavaScript interactions with network items
  4. Ensure no memory leaks or premature object destruction
  5. Verify network view operations work correctly after these changes

fix: 为NetItem设置父对象防止JS释放

  1. 在NetManagerPrivate构造函数中为根项目和删除项目添加setParent()调用
  2. 在NetItemPrivate::addChild()中添加子项目时调用setParent()
  3. 这些更改确保建立正确的父子关系
  4. 防止JavaScript可能提前释放NetItem对象的问题

Influence:

  1. 测试网络项目层次结构在操作期间保持稳定
  2. 验证父子关系是否正确维护
  3. 测试JavaScript与网络项目的交互
  4. 确保没有内存泄漏或对象过早销毁
  5. 验证网络视图操作在这些更改后正常工作

Summary by Sourcery

Ensure NetItem instances are correctly parented to prevent premature destruction in JavaScript integrations.

Bug Fixes:

  • Set NetManager root and delete NetItem objects’ parents to the manager private instance to maintain object lifetimes.
  • Assign parents to child NetItem objects when they are added to preserve the network item hierarchy and avoid premature release.

@sourcery-ai
Copy link

sourcery-ai bot commented Mar 24, 2026

Reviewer's guide (collapsed on small PRs)

Reviewer's Guide

Ensures NetItem instances maintain proper Qt parent-child relationships to prevent premature deletion by JavaScript bindings by setting parents for root/delete items in NetManagerPrivate and for child items in NetItemPrivate::addChild, alongside updating copyright years.

Sequence diagram for NetItemPrivate::addChild parent-child setup

sequenceDiagram
    participant Caller
    participant NetItemPrivate_parent as NetItemPrivate_parent
    participant NetItem_parent as NetItem_parent
    participant NetItemPrivate_child as NetItemPrivate_child
    participant NetItem_child as NetItem_child

    Caller->>NetItemPrivate_parent: addChild(NetItemPrivate_child, index)
    NetItemPrivate_parent->>NetItem_parent: childAboutToBeAdded(NetItem_parent, index)
    NetItemPrivate_parent->>NetItemPrivate_child: item()
    NetItemPrivate_child-->>NetItemPrivate_parent: NetItem_child
    NetItemPrivate_parent->>NetItem_parent: insert child in m_children at index
    NetItemPrivate_parent->>NetItem_child: setParent(NetItem_parent)
    NetItemPrivate_parent->>NetItemPrivate_child: set m_parent = NetItem_parent
    NetItemPrivate_parent->>NetItem_parent: childAdded(NetItem_child)
    NetItemPrivate_parent-->>Caller: return true
Loading

Class diagram for updated NetManagerPrivate and NetItemPrivate parent relationships

classDiagram
    class QObject

    class NetManagerPrivate {
        - bool m_supportWireless
        - NetItem* m_root
        - NetItem* m_deleteItem
        - NetManager* q_ptr
        + NetManagerPrivate(NetManager* manager)
    }

    class NetItem {
        + void setParent(QObject* parent)
        + QObject* parent()
        + void childAboutToBeAdded(NetItem* parent, int index)
        + void childAdded(NetItem* child)
    }

    class NetItemPrivate {
        - NetItem* m_item
        - NetItem* m_parent
        - vector~NetItem*~ m_children
        + bool addChild(NetItemPrivate* child, int index)
        + NetItem* item()
    }

    NetItemPrivate --> NetItem : owns m_item
    NetItemPrivate --> NetItem : m_parent
    NetItemPrivate --> NetItem : m_children

    NetManagerPrivate --> NetItem : m_root
    NetManagerPrivate --> NetItem : m_deleteItem

    QObject <|-- NetItem
    QObject <|-- NetManagerPrivate
Loading

File-Level Changes

Change Details Files
Ensure NetManagerPrivate owns its root and delete NetItem instances via Qt parent-child relationships.
  • In NetManagerPrivate constructor, call setParent(this) on m_root->item() to make the manager private object the QObject parent of the root item.
  • In NetManagerPrivate constructor, call setParent(this) on m_deleteItem->item() to make the manager private object the QObject parent of the delete item.
  • Leave the rest of NetManagerPrivate initialization (updateenabled and addItem calls) unchanged.
net-view/operation/netmanager.cpp
Ensure newly added NetItem children are QObject-children of their logical parent item to keep them alive under JavaScript bindings.
  • After inserting a child item into m_children in NetItemPrivate::addChild, call child->m_item->setParent(m_item) to synchronize the QObject parent-child relationship with the logical hierarchy.
  • Keep existing signals (childAboutToBeAdded, childAdded) and parent pointer assignment intact so behavior is unchanged aside from ownership.
  • Maintain overall addChild logic, only augmenting it with QObject parenting.
net-view/operation/private/netitemprivate.cpp
Refresh SPDX copyright years to include 2026 in modified files.
  • Update the SPDX-FileCopyrightText year range from 2018 - 2022 to 2018 - 2026 in both modified source files.
net-view/operation/netmanager.cpp
net-view/operation/private/netitemprivate.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 - I've found 1 issue, and left some high level feedback:

  • In NetManagerPrivate's constructor, double-check that this is the correct QObject owner for m_root->item() and m_deleteItem->item()—if the public NetManager (q_ptr) is the QObject, you may want to parent these items to q_ptr instead to keep ownership consistent with the public API.
  • In NetItemPrivate::addChild, consider guarding child->m_item->setParent(m_item) against re-parenting an item that already has a parent, or explicitly handling the re-parenting case, to avoid unexpected lifetime or signal/slot side effects.
Prompt for AI Agents
Please address the comments from this code review:

## Overall Comments
- In `NetManagerPrivate`'s constructor, double-check that `this` is the correct `QObject` owner for `m_root->item()` and `m_deleteItem->item()`—if the public `NetManager` (`q_ptr`) is the QObject, you may want to parent these items to `q_ptr` instead to keep ownership consistent with the public API.
- In `NetItemPrivate::addChild`, consider guarding `child->m_item->setParent(m_item)` against re-parenting an item that already has a parent, or explicitly handling the re-parenting case, to avoid unexpected lifetime or signal/slot side effects.

## Individual Comments

### Comment 1
<location path="net-view/operation/private/netitemprivate.cpp" line_range="124" />
<code_context>

     Q_EMIT m_item->childAboutToBeAdded(m_item, index);
     m_children.insert(m_children.begin() + index, child->item());
+    child->m_item->setParent(m_item);

     child->m_parent = m_item;
</code_context>
<issue_to_address>
**suggestion (bug_risk):** Keep QObject parent and logical `m_parent` in sync or derive one from the other to avoid inconsistencies.

Here you now update `QObject` parent (`child->m_item->setParent(m_item)`) and logical parent (`child->m_parent = m_item`) separately, which risks them getting out of sync if any future reparenting only updates one. Please either derive `m_parent` from `QObject::parent()` and drop the manual pointer, or centralize all parent changes in a single helper that updates both together to avoid subtle hierarchy inconsistencies.

Suggested implementation:

```cpp
    Q_EMIT m_item->childAboutToBeAdded(m_item, index);
    m_children.insert(m_children.begin() + index, child->item());
    child->setParentItem(m_item);
    Q_EMIT m_item->childAdded(child->item());

```

To fully implement the centralization and keep `QObject` parent and logical parent in sync, you should also:

1. In the corresponding header for `NetItemPrivate` (likely `netitemprivate.h`), declare a helper method, for example:
   ```cpp
   void setParentItem(QObject *parent);
   ```
   or, if `m_parent` is a more specific type, use that type:
   ```cpp
   void setParentItem(NetItem *parent);
   ```

2. In `net-view/operation/private/netitemprivate.cpp`, **define** this helper so that it updates both the `QObject` parent and the logical `m_parent` in one place, e.g.:
   ```cpp
   void NetItemPrivate::setParentItem(QObject *parent)
   {
       m_item->setParent(parent);
       m_parent = parent;
   }
   ```
   Adjust the parameter type and assignment as needed to match the actual type of `m_parent`.

3. Search for any other places in the codebase where both `m_item->setParent(...)` and `m_parent = ...` (or equivalent) are called separately, and replace those paired operations with a single call to `setParentItem(...)` so that all reparenting goes through this centralized helper.
</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.


Q_EMIT m_item->childAboutToBeAdded(m_item, index);
m_children.insert(m_children.begin() + index, child->item());
child->m_item->setParent(m_item);
Copy link

Choose a reason for hiding this comment

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

suggestion (bug_risk): Keep QObject parent and logical m_parent in sync or derive one from the other to avoid inconsistencies.

Here you now update QObject parent (child->m_item->setParent(m_item)) and logical parent (child->m_parent = m_item) separately, which risks them getting out of sync if any future reparenting only updates one. Please either derive m_parent from QObject::parent() and drop the manual pointer, or centralize all parent changes in a single helper that updates both together to avoid subtle hierarchy inconsistencies.

Suggested implementation:

    Q_EMIT m_item->childAboutToBeAdded(m_item, index);
    m_children.insert(m_children.begin() + index, child->item());
    child->setParentItem(m_item);
    Q_EMIT m_item->childAdded(child->item());

To fully implement the centralization and keep QObject parent and logical parent in sync, you should also:

  1. In the corresponding header for NetItemPrivate (likely netitemprivate.h), declare a helper method, for example:

    void setParentItem(QObject *parent);

    or, if m_parent is a more specific type, use that type:

    void setParentItem(NetItem *parent);
  2. In net-view/operation/private/netitemprivate.cpp, define this helper so that it updates both the QObject parent and the logical m_parent in one place, e.g.:

    void NetItemPrivate::setParentItem(QObject *parent)
    {
        m_item->setParent(parent);
        m_parent = parent;
    }

    Adjust the parameter type and assignment as needed to match the actual type of m_parent.

  3. Search for any other places in the codebase where both m_item->setParent(...) and m_parent = ... (or equivalent) are called separately, and replace those paired operations with a single call to setParentItem(...) so that all reparenting goes through this centralized helper.

caixr23
caixr23 previously approved these changes Mar 24, 2026
1. Added setParent() calls for root and delete items in
NetManagerPrivate constructor
2. Added setParent() call when adding child items in
NetItemPrivate::addChild()
3. These changes ensure proper parent-child relationships are
established
4. Prevents potential issues where JavaScript could release NetItem
objects prematurely

Influence:
1. Test network item hierarchy remains stable during operations
2. Verify parent-child relationships are correctly maintained
3. Test JavaScript interactions with network items
4. Ensure no memory leaks or premature object destruction
5. Verify network view operations work correctly after these changes

fix: 为NetItem设置父对象防止JS释放

1. 在NetManagerPrivate构造函数中为根项目和删除项目添加setParent()调用
2. 在NetItemPrivate::addChild()中添加子项目时调用setParent()
3. 这些更改确保建立正确的父子关系
4. 防止JavaScript可能提前释放NetItem对象的问题

Influence:
1. 测试网络项目层次结构在操作期间保持稳定
2. 验证父子关系是否正确维护
3. 测试JavaScript与网络项目的交互
4. 确保没有内存泄漏或对象过早销毁
5. 验证网络视图操作在这些更改后正常工作
@deepin-ci-robot
Copy link

[APPROVALNOTIFIER] This PR is NOT APPROVED

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

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

@18202781743 18202781743 merged commit d5e89e0 into linuxdeepin:master Mar 24, 2026
17 of 19 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