Skip to content

Conversation

@caixr23
Copy link
Contributor

@caixr23 caixr23 commented Nov 18, 2025

Add memory management feature flag DCC_MEMORY_MANAGEME to control object ownership and deletion behavior. Implement DccQuickRepeater to ensure child items have proper parent relationships. Fix potential crashes when repeater child objects lack parent objects by setting ownership and parent relationships correctly.

Changes include:

  1. Add DCC_MEMORY_MANAGEME compile flag to control memory management features
  2. Create DccQuickRepeater class that automatically sets parent for child items
  3. Replace standard Repeater with DccQuickRepeater in QML files
  4. Improve object ownership management in DccObject child handling
  5. Add conditional memory cleanup in DccManager with safety checks

Log: Fixed crash issue when repeater child objects have no parent objects

Influence:

  1. Test repeater functionality in various modules (accounts, datetime, display, etc.)
  2. Verify no crashes when dynamically adding/removing repeater items
  3. Check memory usage and cleanup during application exit
  4. Test object parent relationships in QML components
  5. Verify timezone dialog and other repeater-based components work correctly

fix: 修复Repeater子对象无父对象导致的崩溃问题

添加内存管理功能标志DCC_MEMORY_MANAGEME来控制对象所有权和删除行为。实现
DccQuickRepeater确保子项具有正确的父子关系。通过正确设置所有权和父子关系
修复当Repeater子对象缺少父对象时可能发生的崩溃问题。

变更包括:

  1. 添加DCC_MEMORY_MANAGEME编译标志控制内存管理功能
  2. 创建DccQuickRepeater类自动为子项设置父对象
  3. 在QML文件中将标准Repeater替换为DccQuickRepeater
  4. 改进DccObject子项处理中的对象所有权管理
  5. 在DccManager中添加带安全检查的条件内存清理

Log: 修复Repeater子对象无父对象导致的崩溃问题

Influence:

  1. 测试各模块中的Repeater功能(账户、时间日期、显示等)
  2. 验证动态添加/删除Repeater项时不会崩溃
  3. 检查应用程序退出时的内存使用和清理情况
  4. 测试QML组件中的对象父子关系
  5. 验证时区对话框和其他基于Repeater的组件正常工作

PMS: BUG-307037

Summary by Sourcery

Introduce a memory management feature flag and a custom QuickRepeater to enforce parent-child relationships, update ownership and cleanup logic to prevent crashes when repeater items lack parents.

New Features:

  • Add DCC_MEMORY_MANAGEME compile flag for conditional memory management
  • Introduce DccQuickRepeater class that automatically sets parent on new child items
  • Replace standard Repeater with DccQuickRepeater in QML and update QML imports

Bug Fixes:

  • Fix crashes caused by repeater items lacking a parent and unsafe memory deletion in DccManager and main

Enhancements:

  • Improve object ownership settings in DccObject child handling via QQmlEngine APIs
  • Use QPointer for m_window to safely track QWindow lifetime

Build:

  • Expand CMake configuration to define DCC_MEMORY_MANAGEME, link Qt Quick Private modules, and default the flag on

@sourcery-ai
Copy link

sourcery-ai bot commented Nov 18, 2025

Reviewer's Guide

This PR adds a compile-time memory management flag to control C++ ownership and safe deletion, introduces a DccQuickRepeater to auto-assign parent relationships for QML repeater items, replaces standard Repeaters in QML modules, and refines object ownership and cleanup in DccObject and DccManager to prevent crashes when children lack parents.

Sequence diagram for DccQuickRepeater item addition and parent assignment

sequenceDiagram
    participant QML as QML Engine
    participant DccQuickRepeater
    participant QQuickItem
    QML->>DccQuickRepeater: Add new item to repeater
    DccQuickRepeater->>QQuickItem: onItemAdded(index, item)
    alt Item has no parent
        DccQuickRepeater->>QQuickItem: setParent(DccQuickRepeater)
    end
Loading

ER diagram for DccObject child-parent relationships and ownership

erDiagram
    DCCOBJECT {
        id int
        parent_id int
        ownership enum
    }
    DCCOBJECT ||--o{ DCCOBJECT : has_children
    DCCOBJECT }o--|| DCCOBJECT : has_parent
Loading

Class diagram for new DccQuickRepeater and related ownership changes

classDiagram
    class DccQuickRepeater {
        +DccQuickRepeater(QQuickItem *parent)
        +~DccQuickRepeater()
        +onItemAdded(int index, QQuickItem *item)
    }
    DccQuickRepeater --|> QQuickRepeater

    class DccObject {
        +addChild(DccObject *child, bool updateParent)
        +getSectionItem(QObject *parent)
        +deleteSectionItem(bool later)
    }
    DccObject o-- "1..*" DccObject : children

    class DccManager {
        +clearData()
        -QPointer<QWindow> m_window
    }
    class QPointer_QWindow
    DccManager o-- QPointer_QWindow : m_window

    QQuickRepeater <|-- DccQuickRepeater
    QQuickItem <|-- DccQuickRepeater
    DccObject "1" o-- "1..*" DccObject : children
    DccManager o-- DccObject : m_root
Loading

File-Level Changes

Change Details Files
Introduce DCC_MEMORY_MANAGEME compile flag for conditional memory management
  • Add DCC_MEMORY_MANAGEME option and default ON in root and module CMakeLists
  • Wrap deletions of DccManager, QmlEngine, and application objects under #ifdef DCC_MEMORY_MANAGEME
  • Switch DccManager::m_window to QPointer and use .get() for safe access in eventFilter
CMakeLists.txt
src/dde-control-center/CMakeLists.txt
src/dde-control-center/dccmanager.cpp
src/dde-control-center/dccmanager.h
src/dde-control-center/main.cpp
Add DccQuickRepeater to auto-assign parents to repeater items
  • Create DccQuickRepeater header defining onItemAdded override
  • Implement itemAdded connection and parent assignment in source
include/dccquickrepeater.h
src/dde-control-center/frame/dccquickrepeater.cpp
Replace standard QML Repeater with DccQuickRepeater in various modules
  • Import org.deepin.dcc module and swap Repeater declarations
  • Adjust QML components to use Dcc.Repeater instead of built-in Repeater
src/plugin-datetime/qml/TimezoneDialog.qml
src/plugin-accounts/qml/AvatarSettingsDialog.qml
src/plugin-accounts/qml/CustomLocalAvatarsRow.qml
src/plugin-display/qml/ScreenTab.qml
src/plugin-personalization/qml/DccColorDialog.qml
src/plugin-personalization/qml/ScreenTab.qml
src/plugin-power/qml/CustomTipsSlider.qml
src/dde-control-center/frame/plugin/DccGroupView.qml
src/dde-control-center/frame/plugin/DccRowView.qml
Refine object ownership and safe deletion in DccObject
  • Set QQmlEngine::CppOwnership for added children
  • Wrap deleteSectionItem logic under DCC_MEMORY_MANAGEME
  • Assign JavaScriptOwnership for section items
src/dde-control-center/frame/dccobject.cpp
Update QML plugin registration and build deps for QuickRepeater
  • Include and register DccQuickRepeater as the 'Repeater' type
  • Add QuickPrivate to plugin and core build dependencies
  • Maintain CppOwnership for DccApp singleton
src/dde-control-center/frame/plugin/src/dccqmlplugin.cpp
src/dde-control-center/frame/plugin/CMakeLists.txt

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 there - I've reviewed your changes and they look great!

Prompt for AI Agents
Please address the comments from this code review:

## Individual Comments

### Comment 1
<location> `src/dde-control-center/frame/plugin/src/dccqmlplugin.cpp:25` </location>
<code_context>
     qmlRegisterType<dccV25::DccModel>(uri, 1, 0, "DccModel");
     qmlRegisterType<dccV25::DccQuickDBusInterface>(uri, 1, 0, "DccDBusInterface");
-    QQmlEngine::setObjectOwnership(dccV25::DccApp::instance(),QQmlEngine::CppOwnership);
+    qmlRegisterType<dccV25::DccQuickRepeater>(uri, 1, 0, "Repeater");
+    QQmlEngine::setObjectOwnership(dccV25::DccApp::instance(), QQmlEngine::CppOwnership);
     qmlRegisterSingletonInstance(uri, 1, 0, "DccApp", dccV25::DccApp::instance());
</code_context>

<issue_to_address>
**issue:** Registering DccQuickRepeater as 'Repeater' may shadow QtQuick's Repeater.

Consider renaming DccQuickRepeater to avoid potential conflicts or confusion with QtQuick's built-in Repeater type in QML.
</issue_to_address>

### Comment 2
<location> `src/dde-control-center/frame/dccquickrepeater.cpp:17-18` </location>
<code_context>
+
+void DccQuickRepeater::onItemAdded(int, QQuickItem *item)
+{
+    if (!item->parent()) {
+        item->setParent(this);
+    }
+}
</code_context>

<issue_to_address>
**issue (bug_risk):** Setting parent in onItemAdded may interfere with QML's item hierarchy.

Check that manually setting the parent does not disrupt QML's automatic handling, which could affect rendering or event flow.
</issue_to_address>

### Comment 3
<location> `CMakeLists.txt:23` </location>
<code_context>
 option(BUILD_TESTING "UNIT test" OFF)
 set(BUILD_EXAMPLES OFF)

+set(DCC_MEMORY_MANAGEME ON)
 # dcc-v23
 set(BUILD_DCC_OLD OFF)
</code_context>

<issue_to_address>
**issue (typo):** Typo in variable name 'DCC_MEMORY_MANAGEME'; should likely be 'DCC_MEMORY_MANAGEMENT'.

Please update the variable name to 'DCC_MEMORY_MANAGEMENT' for consistency.

Suggested implementation:

```
set(DCC_MEMORY_MANAGEMENT ON)

```

```
if(DCC_MEMORY_MANAGEMENT)
  add_definitions(-DDCC_MEMORY_MANAGEMENT)
endif()

```
</issue_to_address>

### Comment 4
<location> `CMakeLists.txt:105` </location>
<code_context>

 add_definitions(-DLOCALE_I18N_PATH="${LOCALE_I18N_PATH}")

+if(DCC_MEMORY_MANAGEME)
+  add_definitions(-DDCC_MEMORY_MANAGEME)
+endif()
</code_context>

<issue_to_address>
**issue (typo):** Typo in conditional variable 'DCC_MEMORY_MANAGEME'; should likely be 'DCC_MEMORY_MANAGEMENT'.

Please correct all instances of the typo to 'DCC_MEMORY_MANAGEMENT'.

Suggested implementation:

```
set(DCC_MEMORY_MANAGEMENT ON)

```

```
if(DCC_MEMORY_MANAGEMENT)
  add_definitions(-DDCC_MEMORY_MANAGEMENT)
endif()

```
</issue_to_address>

### Comment 5
<location> `CMakeLists.txt:106` </location>
<code_context>
 add_definitions(-DLOCALE_I18N_PATH="${LOCALE_I18N_PATH}")

+if(DCC_MEMORY_MANAGEME)
+  add_definitions(-DDCC_MEMORY_MANAGEME)
+endif()
 # 增加安全编译参数
</code_context>

<issue_to_address>
**issue (typo):** Typo in macro definition '-DDCC_MEMORY_MANAGEME'; should likely be '-DDCC_MEMORY_MANAGEMENT'.

Please update the macro to use the correct spelling to ensure consistency and prevent potential build errors.
</issue_to_address>

### Comment 6
<location> `include/dccquickrepeater.h:10` </location>
<code_context>
+#include "private/qquickrepeater_p.h"
+
+namespace dccV25 {
+class DccQuickRepeater : public QQuickRepeater
+{
+    Q_OBJECT
</code_context>

<issue_to_address>
**issue (complexity):** Consider replacing the custom C++ subclass with QML-only logic to handle item parenting.

You don’t actually need a C++ subclass just to catch `onItemAdded` and reparent – you can do it all in QML. Removing `DccQuickRepeater` will save you the header/private include and boilerplate. For example, instead of:

```cpp
// DccQuickRepeater.h
class DccQuickRepeater : public QQuickRepeater {
    Q_OBJECT
    QML_ELEMENT
protected:
    void onItemAdded(int index, QQuickItem *item) override {
        item->setParentItem(someContainer);
    }
};
```

you can use a plain Repeater in QML:

```qml
import QtQuick 2.15

Item {
    id: someContainer

    Repeater {
        id: repeater
        model: myModel
        delegate: MyDelegate { /* ... */ }

        onItemAdded: {
            // exactly the same reparent logic
            item.parent = someContainer
        }
    }
}
```

Or even simpler, push the parent into the delegate itself:

```qml
import QtQuick 2.15

Item {
    id: someContainer

    Repeater {
        model: myModel
        delegate: MyDelegate {
            parent: someContainer
            /* ... */
        }
    }
}
```

Both keep all existing behavior, remove the extra C++ class and private Qt header include, and are easy to maintain.
</issue_to_address>

### Comment 7
<location> `src/dde-control-center/frame/dccquickrepeater.cpp:6` </location>
<code_context>
+
+#include "private/qquickrepeater_p.h"
+
+namespace dccV25 {
+class DccQuickRepeater : public QQuickRepeater
+{
</code_context>

<issue_to_address>
**issue (complexity):** Consider replacing the C++ subclass with a QML-only implementation to simplify the codebase.

```suggestion
Consider removing the entire C++ subclass and handling `itemAdded` in QML. You can keep the same API surface by providing a lightweight QML wrapper instead of custom C++:

Create DccQuickRepeater.qml:
```qml
// DccQuickRepeater.qml
import QtQuick 2.15

Repeater {
    onItemAdded: {
        // reparent if delegate forgot to set parent
        if (!item.parent) item.parent = parent
    }
}
```

– Delete `dccquickrepeater.h`/`.cpp` and its qmlRegisterType.  
– In your QML imports, replace all `DccQuickRepeater { … }` usages with this new QML type.  

This preserves the exact behavior with zero C++ overhead.
</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.

Copy link

Choose a reason for hiding this comment

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

issue: Registering DccQuickRepeater as 'Repeater' may shadow QtQuick's Repeater.

Consider renaming DccQuickRepeater to avoid potential conflicts or confusion with QtQuick's built-in Repeater type in QML.

#include "private/qquickrepeater_p.h"

namespace dccV25 {
class DccQuickRepeater : public QQuickRepeater
Copy link

Choose a reason for hiding this comment

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

issue (complexity): Consider replacing the custom C++ subclass with QML-only logic to handle item parenting.

You don’t actually need a C++ subclass just to catch onItemAdded and reparent – you can do it all in QML. Removing DccQuickRepeater will save you the header/private include and boilerplate. For example, instead of:

// DccQuickRepeater.h
class DccQuickRepeater : public QQuickRepeater {
    Q_OBJECT
    QML_ELEMENT
protected:
    void onItemAdded(int index, QQuickItem *item) override {
        item->setParentItem(someContainer);
    }
};

you can use a plain Repeater in QML:

import QtQuick 2.15

Item {
    id: someContainer

    Repeater {
        id: repeater
        model: myModel
        delegate: MyDelegate { /* ... */ }

        onItemAdded: {
            // exactly the same reparent logic
            item.parent = someContainer
        }
    }
}

Or even simpler, push the parent into the delegate itself:

import QtQuick 2.15

Item {
    id: someContainer

    Repeater {
        model: myModel
        delegate: MyDelegate {
            parent: someContainer
            /* ... */
        }
    }
}

Both keep all existing behavior, remove the extra C++ class and private Qt header include, and are easy to maintain.

// SPDX-License-Identifier: GPL-3.0-or-later
#include "dccquickrepeater.h"

namespace dccV25 {
Copy link

Choose a reason for hiding this comment

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

issue (complexity): Consider replacing the C++ subclass with a QML-only implementation to simplify the codebase.

Suggested change
namespace dccV25 {
Consider removing the entire C++ subclass and handling `itemAdded` in QML. You can keep the same API surface by providing a lightweight QML wrapper instead of custom C++:
Create DccQuickRepeater.qml:
```qml
// DccQuickRepeater.qml
import QtQuick 2.15
Repeater {
onItemAdded: {
// reparent if delegate forgot to set parent
if (!item.parent) item.parent = parent
}
}

– Delete dccquickrepeater.h/.cpp and its qmlRegisterType.
– In your QML imports, replace all DccQuickRepeater { … } usages with this new QML type.

This preserves the exact behavior with zero C++ overhead.

@deepin-bot
Copy link

deepin-bot bot commented Nov 27, 2025

TAG Bot

New tag: 6.1.59
DISTRIBUTION: unstable
Suggest: synchronizing this PR through rebase #2843

@deepin-bot
Copy link

deepin-bot bot commented Nov 28, 2025

TAG Bot

New tag: 6.1.60
DISTRIBUTION: unstable
Suggest: synchronizing this PR through rebase #2844

@deepin-bot
Copy link

deepin-bot bot commented Dec 4, 2025

TAG Bot

New tag: 6.1.61
DISTRIBUTION: unstable
Suggest: synchronizing this PR through rebase #2854

@caixr23 caixr23 force-pushed the master branch 7 times, most recently from 58cbf10 to e46b21c Compare December 9, 2025 02:18
@mhduiy
Copy link
Contributor

mhduiy commented Dec 9, 2025

加一个DccRepeater是为了给item设置一个父对象,Qt不会自动进行清理吗

@caixr23
Copy link
Contributor Author

caixr23 commented Dec 9, 2025

加一个DccRepeater是为了给item设置一个父对象,Qt不会自动进行清理吗

Qt会清理,但时机会靠后。关闭时的当前页面对象会在qApp分析结构时才清理

@deepin-ci-robot
Copy link

[APPROVALNOTIFIER] This PR is NOT APPROVED

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

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

Add memory management feature flag DCC_MEMORY_MANAGEME to control object
ownership and deletion behavior. Implement DccQuickRepeater to ensure
child items have proper parent relationships. Fix potential crashes
when repeater child objects lack parent objects by setting ownership and
parent relationships correctly.

Changes include:
1. Add DCC_MEMORY_MANAGEME compile flag to control memory management
features
2. Create DccQuickRepeater class that automatically sets parent for
child items
3. Replace standard Repeater with DccQuickRepeater in QML files
4. Improve object ownership management in DccObject child handling
5. Add conditional memory cleanup in DccManager with safety checks

Log: Fixed crash issue when repeater child objects have no parent
objects

Influence:
1. Test repeater functionality in various modules (accounts, datetime,
display, etc.)
2. Verify no crashes when dynamically adding/removing repeater items
3. Check memory usage and cleanup during application exit
4. Test object parent relationships in QML components
5. Verify timezone dialog and other repeater-based components work
correctly

fix: 修复Repeater子对象无父对象导致的崩溃问题

添加内存管理功能标志DCC_MEMORY_MANAGEME来控制对象所有权和删除行为。实现
DccQuickRepeater确保子项具有正确的父子关系。通过正确设置所有权和父子关系
修复当Repeater子对象缺少父对象时可能发生的崩溃问题。

变更包括:
1. 添加DCC_MEMORY_MANAGEME编译标志控制内存管理功能
2. 创建DccQuickRepeater类自动为子项设置父对象
3. 在QML文件中将标准Repeater替换为DccQuickRepeater
4. 改进DccObject子项处理中的对象所有权管理
5. 在DccManager中添加带安全检查的条件内存清理

Log: 修复Repeater子对象无父对象导致的崩溃问题

Influence:
1. 测试各模块中的Repeater功能(账户、时间日期、显示等)
2. 验证动态添加/删除Repeater项时不会崩溃
3. 检查应用程序退出时的内存使用和清理情况
4. 测试QML组件中的对象父子关系
5. 验证时区对话框和其他基于Repeater的组件正常工作

PMS: BUG-307037
@deepin-ci-robot
Copy link

deepin pr auto review

我来对这个git diff进行详细的代码审查:

  1. 内存管理改进
    优点:
  • 添加了 DCC_ENABLE_MEMORY_MANAGEMENT 宏来控制内存管理
  • 使用 QPointer 替代裸指针避免悬空指针
  • 在 clearData() 中添加了显式的内存释放
  • 设置了 JavaScript 所有权管理 QML 对象

建议:

  • 在 DccQuickRepeater 中,onItemAdded 方法应该检查 item 的有效性后再设置 parent
  • 建议在 delete m_window 之前先检查 window 是否已经关闭
  • clearData() 中的延时删除操作应该考虑使用 QPointer 来避免访问已删除对象
  1. 代码安全性
    优点:
  • 使用了 QPointer 防止野指针访问
  • 添加了空指针检查
  • 对 QML 对象的所有权进行了明确管理

建议:

  • 在 eventFilter 中应该对 static_cast 的结果进行空指针检查
  • deleteSectionItem 中的延时删除操作需要考虑线程安全
  • 建议在所有 QML 组件中统一使用 Dcc.Repeater 替代原生 Repeater
  1. 代码性能
    建议:
  • 延时删除操作使用 500ms 的固定值,建议改为可配置的
  • QPointer 的使用会增加一些开销,建议评估是否所有地方都需要使用
  1. 代码质量
    优点:
  • 代码结构清晰,职责分明
  • 添加了适当的注释
  • 遵循了 Qt 的编码规范

建议:

  • DccQuickRepeater 类应该添加更详细的文档说明
  • 建议将 DCC_ENABLE_MEMORY_MANAGEMENT 的相关代码集中管理
  • 部分文件中 import 语句的顺序可以优化
  1. 具体修改建议:
// dccmanager.cpp
bool DccManager::eventFilter(QObject *watched, QEvent *event)
{
    if (event->type() == QEvent::MouseButtonPress && watched == m_window && m_window) {
        QMouseEvent *e = static_cast<QMouseEvent *>(event);
        if (e->buttons() == Qt::LeftButton) {
            QQuickWindow *w = qobject_cast<QQuickWindow *>(m_window.get()); // 使用 qobject_cast 替代 static_cast
            if (!w) {
                return false; // 添加类型转换失败的检查
            }
            // ... 其余代码
        }
    }
    return QObject::eventFilter(watched, event);
}

// dccquickrepeater.cpp
void DccQuickRepeater::onItemAdded(int, QQuickItem *item)
{
    if (item && !item->parent()) {
        // 检查 item 是否已经被删除
        if (!QQmlEngine::objectOwnership(item) == QQmlEngine::CppOwnership) {
            item->setParent(this);
        }
    }
}

// dccobject.cpp
void DccObject::Private::deleteSectionItem(bool later)
{
    if (m_sectionItem) {
#ifdef DCC_ENABLE_MEMORY_MANAGEMENT
        QPointer<QQuickItem> item = m_sectionItem.get(); // 使用 QPointer 确保安全
        m_sectionItem = nullptr;
        q_ptr->setParentItem(nullptr);
        
        if (later) {
            // 使用可配置的延时时间
            const int deleteDelay = q_ptr->property("deleteDelay").toInt();
            QTimer::singleShot(deleteDelay > 0 ? deleteDelay : 500, [item]() {
                if (item) { // 检查对象是否仍然存在
                    delete item;
                }
            });
        } else {
            item->deleteLater();
        }
#else
        m_sectionItem = nullptr;
        q_ptr->setParentItem(nullptr);
#endif
    }
}
  1. 其他建议:
  • 考虑添加内存管理的日志记录,便于调试
  • 建议添加单元测试来验证内存管理的正确性
  • 考虑使用 Qt 的对象树机制来简化内存管理
  • 建议在文档中说明 DCC_ENABLE_MEMORY_MANAGEMENT 的使用场景和注意事项

总的来说,这次改动主要改进了内存管理,提高了代码的安全性,但还需要注意一些细节问题,特别是在对象生命周期管理和线程安全方面。

@caixr23
Copy link
Contributor Author

caixr23 commented Dec 9, 2025

/forcemerge

@deepin-bot
Copy link

deepin-bot bot commented Dec 9, 2025

This pr force merged! (status: blocked)

@deepin-bot deepin-bot bot merged commit 40fd695 into linuxdeepin:master Dec 9, 2025
16 of 18 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