Skip to content

fix(reader): remove unsafe QObject* logging in SideBarImageViewModel - #193

Merged
deepin-bot[bot] merged 1 commit into
linuxdeepin:masterfrom
dengzhongyuan365-dev:master
Sep 24, 2025
Merged

fix(reader): remove unsafe QObject* logging in SideBarImageViewModel#193
deepin-bot[bot] merged 1 commit into
linuxdeepin:masterfrom
dengzhongyuan365-dev:master

Conversation

@dengzhongyuan365-dev

Copy link
Copy Markdown
Member
  • Remove qCDebug statements that printed Annotation*/QObject* in SideBarImageViewModel::removeItemForAnno and findItemForAnno Drop the ad-hoc test block that logged deleted QObject* to reproduce crashes
  • Avoids implicit dereference by Qt’s debug stream overloads that could crash

bug: https://pms.uniontech.com/bug-view-335473.html

* Remove qCDebug statements that printed Annotation*/QObject* in
SideBarImageViewModel::removeItemForAnno and findItemForAnno
Drop the ad-hoc test block that logged deleted QObject* to reproduce crashes
* Avoids implicit dereference by Qt’s debug stream overloads that could crash

bug: https://pms.uniontech.com/bug-view-335473.html
@deepin-ci-robot

Copy link
Copy Markdown

deepin pr auto review

我来分析这段代码的改进意见:

  1. 代码逻辑:
  • removeItemForAnno函数中的日志记录被移除了,这可能会影响调试和问题追踪
  • findItemForAnno函数中的日志记录也被移除了,同样会影响调试
  • 两个函数的核心逻辑保持不变,移除日志记录可能会降低代码的可维护性
  1. 代码质量:
  • 移除所有调试日志可能会影响问题的快速定位和排查
  • 建议保留关键节点的日志记录,或者使用条件编译来控制日志的输出
  • 函数命名规范,注释清晰,整体结构合理
  1. 代码性能:
  • findItemForAnno函数使用线性查找,时间复杂度为O(n)
  • 如果m_pagelst可能很大,建议考虑使用哈希表等数据结构来优化查找性能
  • removeItemForAnno函数中使用了beginResetModel()和endResetModel(),这会导致整个模型重置,可能会影响UI性能
  1. 代码安全:
  • 没有看到对annotation参数的空指针检查
  • 建议在函数开始时添加参数有效性检查
  • 在访问m_pagelst时没有看到线程同步机制,如果可能在多线程环境下使用,需要考虑线程安全

改进建议:

  1. 建议恢复关键节点的日志记录,或者使用条件编译:
void SideBarImageViewModel::removeItemForAnno(deepin_reader::Annotation *annotation)
{
    Q_ASSERT(annotation != nullptr);
    qCDebug(appLog) << "Removing item for annotation:" << annotation;
    int index = findItemForAnno(annotation);
    if (index >= 0) {
        qCDebug(appLog) << "Removing item for annotation:" << annotation << "found";
        beginResetModel();
        m_pagelst.removeAt(index);
        endResetModel();
    }
    qCDebug(appLog) << "Removing item for annotation:" << annotation << "end";
}
  1. 考虑优化查找性能,可以使用QHash来建立annotation到index的映射:
class SideBarImageViewModel {
private:
    QList<ImagePageInfo> m_pagelst;
    QHash<deepin_reader::Annotation*, int> m_annoIndexMap;
    
    void updateAnnoIndexMap() {
        m_annoIndexMap.clear();
        for (int i = 0; i < m_pagelst.size(); ++i) {
            m_annoIndexMap.insert(m_pagelst[i].annotation, i);
        }
    }
    
public:
    int findItemForAnno(deepin_reader::Annotation *annotation) {
        Q_ASSERT(annotation != nullptr);
        return m_annoIndexMap.value(annotation, -1);
    }
    
    void removeItemForAnno(deepin_reader::Annotation *annotation) {
        Q_ASSERT(annotation != nullptr);
        int index = findItemForAnno(annotation);
        if (index >= 0) {
            beginResetModel();
            m_pagelst.removeAt(index);
            updateAnnoIndexMap();
            endResetModel();
        }
    }
};
  1. 考虑线程安全,可以使用QMutex保护数据访问:
class SideBarImageViewModel {
private:
    QMutex m_mutex;
    QList<ImagePageInfo> m_pagelst;
    
    // ... 其他成员 ...
    
    int findItemForAnno(deepin_reader::Annotation *annotation) {
        QMutexLocker locker(&m_mutex);
        // ... 原有逻辑 ...
    }
    
    void removeItemForAnno(deepin_reader::Annotation *annotation) {
        QMutexLocker locker(&m_mutex);
        // ... 原有逻辑 ...
    }
};
  1. 考虑使用更高效的模型更新方式:
void removeItemForAnno(deepin_reader::Annotation *annotation) {
    Q_ASSERT(annotation != nullptr);
    int index = findItemForAnno(annotation);
    if (index >= 0) {
        beginRemoveRows(QModelIndex(), index, index);
        m_pagelst.removeAt(index);
        endRemoveRows();
    }
}

这种方式只更新被删除的行,而不是整个模型,性能会更好。

@deepin-ci-robot

Copy link
Copy Markdown

[APPROVALNOTIFIER] This PR is NOT APPROVED

This pull-request has been approved by: dengzhongyuan365-dev, lzwind

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

@dengzhongyuan365-dev

Copy link
Copy Markdown
Member Author

/forcemerge

@deepin-bot
deepin-bot Bot merged commit b8625f7 into linuxdeepin:master Sep 24, 2025
6 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