Skip to content

Conversation

@hawk0620
Copy link

@hawk0620 hawk0620 commented Jul 14, 2025

New Pull Request Checklist

  • I have read and understood the CONTRIBUTING guide

  • I have read the Documentation

  • I have searched for a similar pull request in the project and found none

  • I have updated this branch with the latest master to avoid conflicts (via merge from master or rebase)

  • I have added the required tests to prove the fix/feature I am adding

  • I have updated the documentation (if necessary)

  • I have run the tests and they pass

  • I have run the lint and it passes (pod lib lint)

This merge request fixes / refers to the following issues: ...

Pull Request Description

Add condition statement if (!self) { return; } to avoid self being nil in block and ensure the logic executes only when self is valid.

Summary by CodeRabbit

  • Bug Fixes
    • Improved app stability by preventing potential crashes when certain image loading or animation operations are performed on deallocated views or objects.

@coderabbitai
Copy link

coderabbitai bot commented Jul 14, 2025

Walkthrough

The changes introduce explicit nil checks for self after weak-to-strong reference promotion within completion or handler blocks across several files. This ensures that if self has been deallocated, the block exits early, preventing operations on a nil object and avoiding potential crashes or undefined behavior.

Changes

File(s) Change Summary
SDWebImage/Core/SDAnimatedImageView.m Added nil checks for self in animationFrameHandler and animationLoopHandler blocks.
SDWebImage/Core/SDImageCachesManager.m Removed @weakify/@strongify macros from all serial operation completion blocks.
SDWebImage/Core/UIButton+WebCache.m Added nil check for self in setImageBlock within sd_setImageWithURL:forState:... method.
SDWebImage/Core/UIImageView+HighlightedWebCache.m Added nil check for self in setImageBlock within sd_setHighlightedImageWithURL:... method.

Poem

A hop and a skip through the code we go,
Checking for nil, so crashes stay low.
With blocks now guarded, the bugs disappear,
The rabbits rejoice, the path is clear!
Safe and sound, our images show—
Hippity-hop, SDWebImage in tow!
🐇✨


📜 Recent review details

Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between fdea700 and 7731fbd.

📒 Files selected for processing (1)
  • SDWebImage/Core/SDImageCachesManager.m (0 hunks)
💤 Files with no reviewable changes (1)
  • SDWebImage/Core/SDImageCachesManager.m

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share
🪧 Tips

Chat

There are 3 ways to chat with CodeRabbit:

  • Review comments: Directly reply to a review comment made by CodeRabbit. Example:
    • I pushed a fix in commit <commit_id>, please review it.
    • Explain this complex logic.
    • Open a follow-up GitHub issue for this discussion.
  • Files and specific lines of code (under the "Files changed" tab): Tag @coderabbitai in a new review comment at the desired location with your query. Examples:
    • @coderabbitai explain this code block.
    • @coderabbitai modularize this function.
  • PR comments: Tag @coderabbitai in a new PR comment to ask questions about the PR branch. For the best results, please provide a very specific query, as very limited context is provided in this mode. Examples:
    • @coderabbitai gather interesting stats about this repository and render them as a table. Additionally, render a pie chart showing the language distribution in the codebase.
    • @coderabbitai read src/utils.ts and explain its main purpose.
    • @coderabbitai read the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.
    • @coderabbitai help me debug CodeRabbit configuration file.

Support

Need help? Create a ticket on our support page for assistance with any issues or questions.

Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments.

CodeRabbit Commands (Invoked using PR comments)

  • @coderabbitai pause to pause the reviews on a PR.
  • @coderabbitai resume to resume the paused reviews.
  • @coderabbitai review to trigger an incremental review. This is useful when automatic reviews are disabled for the repository.
  • @coderabbitai full review to do a full review from scratch and review all the files again.
  • @coderabbitai summary to regenerate the summary of the PR.
  • @coderabbitai generate docstrings to generate docstrings for this PR.
  • @coderabbitai generate sequence diagram to generate a sequence diagram of the changes in this PR.
  • @coderabbitai resolve resolve all the CodeRabbit review comments.
  • @coderabbitai configuration to show the current CodeRabbit configuration for the repository.
  • @coderabbitai help to get help.

Other keywords and placeholders

  • Add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.
  • Add @coderabbitai summary to generate the high-level summary at a specific location in the PR description.
  • Add @coderabbitai anywhere in the PR title to generate the title automatically.

CodeRabbit Configuration File (.coderabbit.yaml)

  • You can programmatically configure CodeRabbit by adding a .coderabbit.yaml file to the root of your repository.
  • Please see the configuration documentation for more information.
  • If your editor has YAML language server enabled, you can add the path at the top of this file to enable auto-completion and validation: # yaml-language-server: $schema=https://coderabbit.ai/integrations/schema.v2.json

Documentation and Community

  • Visit our Documentation for detailed information on how to use CodeRabbit.
  • Join our Discord Community to get help, request features, and share feedback.
  • Follow us on X/Twitter for updates and announcements.

Copy link

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

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

Actionable comments posted: 0

🧹 Nitpick comments (1)
SDWebImage/Core/UIButton+WebCache.m (1)

143-144: Inconsistent nil check pattern.

This @strongify(self) doesn't have the same nil check pattern that was applied to the image callback at lines 76-78. For consistency, consider adding the same defensive check:

 @strongify(self);
+if (!self) {
+    return;
+}
 [self setBackgroundImage:image forState:state];
📜 Review details

Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between b62cb63 and fdea700.

📒 Files selected for processing (4)
  • SDWebImage/Core/SDAnimatedImageView.m (1 hunks)
  • SDWebImage/Core/SDImageCachesManager.m (5 hunks)
  • SDWebImage/Core/UIButton+WebCache.m (1 hunks)
  • SDWebImage/Core/UIImageView+HighlightedWebCache.m (1 hunks)
⏰ Context from checks skipped due to timeout of 90000ms. You can increase the timeout in your CodeRabbit configuration to a maximum of 15 minutes (900000ms). (5)
  • GitHub Check: Unit Test (macOS)
  • GitHub Check: Cocoapods Lint
  • GitHub Check: Unit Test (visionOS)
  • GitHub Check: Unit Test (tvOS)
  • GitHub Check: Cocoapods Demo
🔇 Additional comments (9)
SDWebImage/Core/UIButton+WebCache.m (1)

76-78: Good defensive programming practice.

The nil check after @strongify(self) prevents potential crashes if the object has been deallocated while the block was waiting to execute.

SDWebImage/Core/SDAnimatedImageView.m (2)

245-247: Excellent defensive programming.

The nil check prevents frame updates on a deallocated object, which is crucial for animation handlers that may execute asynchronously.


254-256: Proper safety check for animation loop handling.

The nil check ensures that loop count updates and progressive animation logic only execute when the object is still valid.

SDWebImage/Core/UIImageView+HighlightedWebCache.m (1)

63-65: Good defensive programming practice.

The nil check prevents setting highlightedImage on a deallocated object, which is important for asynchronous image loading scenarios.

SDWebImage/Core/SDImageCachesManager.m (5)

447-449: Essential safety check for serial cache operations.

The nil check prevents recursive calls to serialQueryImageForKey on a deallocated manager, which is crucial for the operation chain integrity.


485-487: Proper defensive programming for cache store operations.

The nil check ensures that serial store operations don't continue processing on a deallocated manager.


506-508: Good safety practice for cache removal operations.

The nil check prevents continuing the serial removal chain on a deallocated manager.


529-531: Appropriate safety check for cache query operations.

The nil check prevents recursive calls to serialContainsImageForKey on a deallocated manager.


567-569: Essential protection for cache clear operations.

The nil check prevents continuing the serial clear chain on a deallocated manager.

@dreampiggy
Copy link
Contributor

Seems I made some mistake. Your original commit is correct. You can just rebase and keep only that fdea700

@hawk0620 hawk0620 force-pushed the opt/avoid_self_be_nil_in_block branch from 7731fbd to fdea700 Compare July 21, 2025 15:02
@dreampiggy dreampiggy merged commit 8c40781 into SDWebImage:master Jul 22, 2025
13 of 15 checks passed
@dreampiggy dreampiggy added this to the 5.21.2 milestone Aug 29, 2025
@tmm1
Copy link

tmm1 commented Sep 24, 2025

Is this for fixing a crash? Or to improve performance?

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