Skip to content

fix: prevent crash during preview item handling#512

Merged
zccrs merged 1 commit intolinuxdeepin:masterfrom
wineee:fix-
Aug 14, 2025
Merged

fix: prevent crash during preview item handling#512
zccrs merged 1 commit intolinuxdeepin:masterfrom
wineee:fix-

Conversation

@wineee
Copy link
Member

@wineee wineee commented Aug 13, 2025

  1. Replace WWrapPointer with QPointer for SurfaceWrapper
  2. Add shellSurface() check before accessing preview item properties
  3. Fix race condition where SurfaceWrapper might be invalid

The changes prevent a crash that occurred when accessing a SurfaceWrapper object after it had been invalidated. Using QPointer with an explicit shellSurface() validity check ensures we don't access dangling pointers, as the SurfaceWrapper::aboutToBeInvalidated signal doesn't nullify WWrapPointer in time.

fix: 修复预览项处理时的崩溃问题

  1. 将 SurfaceWrapper 的指针类型从 WWrapPointer 改为 QPointer
  2. 在访问预览项属性前增加 shellSurface() 有效性检查
  3. 解决 SurfaceWrapper 被标记为无效后仍被访问的竞态条件

这些修改解决了在 SurfaceWrapper 对象被销毁后仍然尝试访问其属性导致的崩溃
问题。使用 QPointer 并配合显式的 shellSurface() 有效性检查,可以确保不会
访问到悬空指针,因为 SurfaceWrapper::aboutToBeInvalidated 信号无法及时将 WWrapPointer 置空。

Summary by Sourcery

Prevent crashes during workspace preview by replacing WWrapPointer with QPointer and adding explicit shellSurface() validity checks

Bug Fixes:

  • Track SurfaceWrapper validity with QPointer instead of WWrapPointer to avoid dangling pointers
  • Add shellSurface() check before accessing preview item properties to prevent race condition crashes

1. Replace WWrapPointer with QPointer for SurfaceWrapper
2. Add shellSurface() check before accessing preview item properties
3. Fix race condition where SurfaceWrapper might be invalid

The changes prevent a crash that occurred when accessing a
SurfaceWrapper object after it had been invalidated. Using QPointer
with an explicit shellSurface() validity check ensures we don't access
dangling pointers, as the SurfaceWrapper::aboutToBeInvalidated signal
doesn't nullify WWrapPointer in time.

fix: 修复预览项处理时的崩溃问题

1. 将 SurfaceWrapper 的指针类型从 WWrapPointer 改为 QPointer
2. 在访问预览项属性前增加 shellSurface() 有效性检查
3. 解决 SurfaceWrapper 被标记为无效后仍被访问的竞态条件

这些修改解决了在 SurfaceWrapper 对象被销毁后仍然尝试访问其属性导致的崩溃
问题。使用 QPointer 并配合显式的 shellSurface() 有效性检查,可以确保不会
访问到悬空指针,因为 SurfaceWrapper::aboutToBeInvalidated 信号无法及时将
WWrapPointer 置空。
@sourcery-ai
Copy link

sourcery-ai bot commented Aug 13, 2025

Reviewer's guide (collapsed on small PRs)

Reviewer's Guide

This pull request replaces the custom WWrapPointer with Qt’s QPointer for tracking SurfaceWrapper, and introduces an explicit shellSurface() validity check in the preview flow to eliminate a race condition that could lead to dangling pointer access.

Sequence diagram for preview item handling with validity check

sequenceDiagram
    participant Workspace
    participant SurfaceWrapper
    participant Model
    Workspace->>SurfaceWrapper: Check shellSurface() validity
    alt shellSurface() is valid
        Workspace->>Model: modelFromId(workspaceId())
        Workspace->>SurfaceWrapper: setOpacity(...)
        Workspace->>SurfaceWrapper: setHideByWorkspace(...)
    else shellSurface() is invalid
        Workspace-->>Workspace: Skip property access
    end
Loading

Class diagram for Workspace pointer management update

classDiagram
    class Workspace {
        - QPointer<SurfaceWrapper> m_previewingItem
        + void startPreviewing(SurfaceWrapper *previewingItem)
    }
    class SurfaceWrapper {
        + shellSurface()
        + setOpacity(double)
        + setHideByWorkspace(bool)
    }
    Workspace --> SurfaceWrapper : QPointer
Loading

File-Level Changes

Change Details Files
Switch m_previewingItem pointer type to QPointer
  • Replaced WWrapPointer include with QPointer
  • Changed m_previewingItem member from WWrapPointer to QPointer
src/workspace/workspace.h
Add shellSurface() validity check in startPreviewing
  • Added "&& m_previewingItem->shellSurface()" condition before using the preview item
  • Updated comment to explain why shellSurface() must be checked instead of relying on aboutToBeInvalidated
src/workspace/workspace.cpp

Possibly linked issues

  • Init repository #1: The PR fixes a crash in preview item handling by preventing access to invalid SurfaceWrapper objects, directly addressing the issue's null pointer crash.

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 @wineee - I've reviewed your changes - here's some feedback:

  • Remove the obsolete TODO and consolidate SurfaceWrapper invalidation logic instead of relying on ad-hoc shellSurface() checks in callers.
  • Encapsulate the shellSurface() validity check into a small helper method to improve readability and avoid duplicating the check.
  • Consider resetting m_previewingItem in SurfaceWrapper::aboutToBeInvalidated so QPointer is nullified automatically and caller code can be simplified.
Prompt for AI Agents
Please address the comments from this code review:
## Overall Comments
- Remove the obsolete TODO and consolidate SurfaceWrapper invalidation logic instead of relying on ad-hoc shellSurface() checks in callers.
- Encapsulate the shellSurface() validity check into a small helper method to improve readability and avoid duplicating the check.
- Consider resetting m_previewingItem in SurfaceWrapper::aboutToBeInvalidated so QPointer is nullified automatically and caller code can be simplified.

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

[APPROVALNOTIFIER] This PR is NOT APPROVED

This pull-request has been approved by: wineee, zccrs

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

@zccrs zccrs merged commit 4cb9bd7 into linuxdeepin:master Aug 14, 2025
13 of 16 checks passed
@wineee wineee deleted the fix- branch August 14, 2025 08:45
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