Skip to content

fix: fix cache image error#3011

Open
caixr23 wants to merge 1 commit intolinuxdeepin:masterfrom
caixr23:master
Open

fix: fix cache image error#3011
caixr23 wants to merge 1 commit intolinuxdeepin:masterfrom
caixr23:master

Conversation

@caixr23
Copy link
Contributor

@caixr23 caixr23 commented Feb 6, 2026

Fixed a bug in the cache image function where a nullptr was being passed as the CacheImageResponse parameter, which could cause crashes or incorrect behavior when caching images. Now properly creates a new CacheImageResponse object with the appropriate parameters.

The change ensures that when caching images without a specific response object, a valid CacheImageResponse is created with the image ID and default size parameters, preventing potential null pointer issues and ensuring proper image caching functionality.

Influence:

  1. Test image loading and caching functionality in control center
  2. Verify that images are properly cached without crashes
  3. Check memory usage to ensure no leaks from new CacheImageResponse objects
  4. Test with various image sizes and formats
  5. Verify thumbnail generation works correctly

fix: 修复缓存图片错误

修复了缓存图片函数中的一个bug,该bug会导致在缓存图片时传递nullptr作为
CacheImageResponse参数,可能引起崩溃或图片缓存不正确的问题。现在正确创建
了带有适当参数的CacheImageResponse对象。

此修改确保在没有特定响应对象时缓存图片,会创建一个包含图片ID和默认尺寸
参数的有效CacheImageResponse,防止潜在的空指针问题并确保图片缓存功能正常
工作。

Influence:

  1. 测试控制中心中的图片加载和缓存功能
  2. 验证图片缓存功能正常且不会崩溃
  3. 检查内存使用情况,确保新的CacheImageResponse对象不会导致内存泄漏
  4. 测试各种图片尺寸和格式
  5. 验证缩略图生成功能正常工作

Fixes: #340297

Summary by Sourcery

Bug Fixes:

  • Resolve a crash and incorrect image caching caused by passing a null CacheImageResponse when caching images without an explicit response object.

Fixed a bug in the cache image function where a nullptr was being
passed as the CacheImageResponse parameter, which could cause crashes
or incorrect behavior when caching images. Now properly creates a new
CacheImageResponse object with the appropriate parameters.

The change ensures that when caching images without a specific response
object, a valid CacheImageResponse is created with the image ID and
default size parameters, preventing potential null pointer issues and
ensuring proper image caching functionality.

Influence:
1. Test image loading and caching functionality in control center
2. Verify that images are properly cached without crashes
3. Check memory usage to ensure no leaks from new CacheImageResponse
objects
4. Test with various image sizes and formats
5. Verify thumbnail generation works correctly

fix: 修复缓存图片错误

修复了缓存图片函数中的一个bug,该bug会导致在缓存图片时传递nullptr作为
CacheImageResponse参数,可能引起崩溃或图片缓存不正确的问题。现在正确创建
了带有适当参数的CacheImageResponse对象。

此修改确保在没有特定响应对象时缓存图片,会创建一个包含图片ID和默认尺寸
参数的有效CacheImageResponse,防止潜在的空指针问题并确保图片缓存功能正常
工作。

Influence:
1. 测试控制中心中的图片加载和缓存功能
2. 验证图片缓存功能正常且不会崩溃
3. 检查内存使用情况,确保新的CacheImageResponse对象不会导致内存泄漏
4. 测试各种图片尺寸和格式
5. 验证缩略图生成功能正常工作

Fixes: #340297
@deepin-ci-robot
Copy link

[APPROVALNOTIFIER] This PR is NOT APPROVED

This pull-request has been approved by: 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

@sourcery-ai
Copy link

sourcery-ai bot commented Feb 6, 2026

Reviewer's guide (collapsed on small PRs)

Reviewer's Guide

Ensures image caching always uses a valid CacheImageResponse by instantiating one instead of passing nullptr in the two-argument cacheImage overload, preventing potential null-pointer crashes and incorrect caching behavior.

Sequence diagram for updated cacheImage overload behavior

sequenceDiagram
    actor Client
    participant DccImageProvider
    participant CacheImageResponse

    Client->>DccImageProvider: cacheImage(id, thumbnailSize)
    activate DccImageProvider
    DccImageProvider->>CacheImageResponse: new CacheImageResponse(id, QSize(), this)
    activate CacheImageResponse
    CacheImageResponse-->>DccImageProvider: instance
    deactivate CacheImageResponse
    DccImageProvider->>DccImageProvider: cacheImage(id, thumbnailSize, response, QSize())
    DccImageProvider-->>Client: QImage pointer
    deactivate DccImageProvider
Loading

Class diagram for DccImageProvider cacheImage change

classDiagram
    class DccImageProvider {
        +~DccImageProvider()
        +QImage* cacheImage(QString id, QSize thumbnailSize)
        +QImage* cacheImage(QString id, QSize thumbnailSize, CacheImageResponse* response, QSize requestedSize)
    }

    class CacheImageResponse {
        +CacheImageResponse(QString id, QSize size, DccImageProvider* provider)
        +QString imageId
        +QSize requestedSize
        +DccImageProvider* owner
    }

    DccImageProvider --> CacheImageResponse : creates
Loading

File-Level Changes

Change Details Files
Always create and pass a valid CacheImageResponse in the convenience cacheImage overload instead of nullptr to avoid null-pointer issues during image caching.
  • Updated the two-argument cacheImage overload to delegate to the full overload with a newly constructed CacheImageResponse instance
  • Initialized the new CacheImageResponse with the image ID, default QSize, and this as the parent to align with expected ownership and default sizing behavior
src/dde-control-center/plugin/dccimageprovider.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 left some high level feedback:

  • The new new CacheImageResponse(...) in cacheImage(const QString &, const QSize &) introduces a raw-allocated object with unclear ownership; consider using a smart pointer or ensuring the callee takes responsibility for deleting it to avoid leaks.
  • When creating the default CacheImageResponse, you pass QSize() rather than the thumbnailSize argument; double-check whether the response should be initialized with the requested thumbnail size instead of an empty size.
Prompt for AI Agents
Please address the comments from this code review:

## Overall Comments
- The new `new CacheImageResponse(...)` in `cacheImage(const QString &, const QSize &)` introduces a raw-allocated object with unclear ownership; consider using a smart pointer or ensuring the callee takes responsibility for deleting it to avoid leaks.
- When creating the default `CacheImageResponse`, you pass `QSize()` rather than the `thumbnailSize` argument; double-check whether the response should be initialized with the requested thumbnail size instead of an empty size.

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.

@deepin-ci-robot
Copy link

deepin pr auto review

针对这段代码的修改,我提供以下审查意见和改进建议:

1. 代码逻辑与内存安全 (严重问题)

问题分析
原代码中 nullptr 被替换为了 new CacheImageResponse(...)。这引入了一个严重的内存泄漏风险。

  • 在 C++ 中,new 分配的内存必须由对应的 delete 释放。
  • 这里 new CacheImageResponse 创建了一个堆对象,并将其指针传递给了 cacheImage 的重载函数。
  • 如果 cacheImage 的实现没有接管这个对象的所有权(例如在析构时删除它),或者没有将其传递给 Qt 的父子对象系统(如 QObject 的父子关系),这块内存将永远无法被回收。

改进建议

  • 方案 A (推荐):使用智能指针(如 QSharedPointerstd::unique_ptr)来管理生命周期,确保资源自动释放。
  • 方案 B:如果 CacheImageResponse 继承自 QObject,且 this (DccImageProvider) 也继承自 QObject,请务必在 new 时设置父对象,即 new CacheImageResponse(id, QSize(), this)。代码中已经包含了 this,需确认 cacheImage 函数内部是否正确处理了对象树关系。
  • 方案 C:检查 CacheImageResponse 的设计。如果它只是为了传递回调或数据,考虑是否可以使用栈对象(局部变量)代替堆对象。

2. 代码质量与可读性

问题分析

  • new CacheImageResponse(id, QSize(), this) 中的 QSize() 是一个空的尺寸对象。如果逻辑上需要传递空尺寸,建议使用命名常量或添加注释说明意图,否则会让代码阅读者困惑。

改进建议

// 假设空尺寸有特定含义,建议明确
QSize invalidSize; 
return cacheImage(id, thumbnailSize, new CacheImageResponse(id, invalidSize, this), invalidSize);

3. 代码性能

问题分析

  • 频繁在堆上分配小对象(如 CacheImageResponse)可能会导致内存碎片,影响性能。
  • 如果 cacheImage 被高频调用,这种分配方式会成为瓶颈。

改进建议

  • 评估 CacheImageResponse 的生命周期。如果它只在函数调用期间有效,强烈建议改为在栈上创建对象,并传递引用或指针(不涉及所有权转移)。

4. 修改后的建议代码

假设 CacheImageResponse 必须是堆对象且需要被 cacheImage 接管所有权,建议修改如下:

QImage *DccImageProvider::cacheImage(const QString &id, const QSize &thumbnailSize)
{
    // 使用栈对象传递参数,避免在此处分配内存
    // 假设 cacheImage 内部会根据需要创建 response,或者逻辑上不需要 response
    // 如果必须传递,请确保所有权清晰
    return cacheImage(id, thumbnailSize, nullptr, QSize());
}

如果必须创建对象,请确保 cacheImage 函数签名或文档明确说明了谁负责删除这个指针:

QImage *DccImageProvider::cacheImage(const QString &id, const QSize &thumbnailSize)
{
    // 明确注释:cacheImage 接管 response 的所有权
    auto *response = new CacheImageResponse(id, QSize(), this);
    return cacheImage(id, thumbnailSize, response, QSize());
}

总结

这段代码修改最大的风险在于内存泄漏。除非 CacheImageResponse 被正确地挂载到 Qt 对象树中或被显式释放,否则这是一个危险的修改。建议回退此改动,或者提供 CacheImageResponse 内存管理的完整上下文。

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.

2 participants