Skip to content

Conversation

@jiangleisir
Copy link

@jiangleisir jiangleisir commented May 14, 2025

This merge request fixes / refers to the following issues:

Thread 39 name:
Thread 39 Crashed:
0 libsystem_platform.dylib 0x00000001f3f6691c _platform_memmove + 76 (:-1)
1 ImageIO 0x00000001ac462d58 GIFBufferInfo::GIFBufferInfo(unsigned char*, bool, unsigned int, unsigned int, unsigned int) + 108 (GIF_common.cpp:38)
2 ImageIO 0x00000001ac474b34 std::__1::__shared_ptr_emplace<GIFBufferInfo, std::__1::allocator >::__shared_ptr_emplace<unsigned char*&, bool, unsigned int&, unsigned int&, unsigned int>(std::__1::allocator<GIFBu... + 76 (shared_ptr.h:298)
3 ImageIO 0x00000001ac474ab4 std::__1::shared_ptr std::__1::allocate_shared<GIFBufferInfo, std::__1::allocator, unsigned char*&, bool, unsigned int&, unsigned int&, unsigned int, void>(std::__1::a... + 88 (shared_ptr.h:292)
4 ImageIO 0x00000001ac4743a4 GIFReadPlugin::copyImageBlockSet(InfoRec*, CGImageProvider*, CGRect, CGSize, __CFDictionary const*) + 800 (shared_ptr.h:1115)
5 ImageIO 0x00000001ac294ef4 IIO_Reader::CopyImageBlockSetProc(void*, CGImageProvider*, CGRect, CGSize, __CFDictionary const*) + 280 (IIOReader.cpp:1444)
6 ImageIO 0x00000001ac2909d0 IIOImageProviderInfo::copyImageBlockSetWithOptions(CGImageProvider*, CGRect, CGSize, __CFDictionary const*) + 744 (CGImagePlus.cpp:2321)
7 ImageIO 0x00000001ac298acc IIOImageProviderInfo::CopyImageBlockSetWithOptions(void*, CGImageProvider*, CGRect, CGSize, __CFDictionary const*) + 828 (CGImagePlus.cpp:2630)
8 CoreGraphics 0x00000001a8b92da4 imageProvider_retain_data + 92 (CGDataProviderImageProvider.c:91)
9 CoreGraphics 0x00000001a8bb3cac CGDataProviderRetainData + 80 (CGDataProvider.c:921)
10 CoreGraphics 0x00000001a8bd1930 provider_for_destination_retain_data + 28 (CGDataProviderForDestination.c:705)
11 CoreGraphics 0x00000001a8bb3cac CGDataProviderRetainData + 80 (CGDataProvider.c:921)
12 CoreGraphics 0x00000001a8b8fb18 CGAccessSessionCreate + 108 (CGAccessSession.c:71)
13 CoreGraphics 0x00000001a8b57f00 img_data_lock + 2364 (CGSImage.c:4934)
14 CoreGraphics 0x00000001a8ba369c CGSImageDataLock + 1320 (CGSImage.c:5847)
15 CoreGraphics 0x00000001a8b72718 ripc_AcquireRIPImageData + 728 (RIPImage.c:337)
16 CoreGraphics 0x00000001a8b91f7c ripc_DrawImage + 804 (RIPContext.c:1432)
17 CoreGraphics 0x00000001a8b645ac CGContextDrawImageWithOptions + 1380 (CGContextDelegate.c:472)
18 MyApp 0x00000001026afc28 +[SDImageCoderHelper CGImageCreateDecoded:orientation:] + 660 (SDImageCoderHelper.m:481)
19 MyApp 0x00000001026b3840 +[SDImageIOAnimatedCoder createFrameAtIndex:source:scale:preserveAspectRatio:thumbnailSize:lazyDecode:animatedImage:decodeToHDR:] + 1172 (SDImageIOAnimatedCoder.m:535)
20 MyApp 0x00000001026b5744 -[SDImageIOAnimatedCoder safeAnimatedImageFrameAtIndex:] + 108 (SDImageIOAnimatedCoder.m:1149)
21 MyApp 0x00000001026b5628 -[SDImageIOAnimatedCoder animatedImageFrameAtIndex:] + 92 (SDImageIOAnimatedCoder.m:1143)
22 MyApp 0x000000010269fb64 -[SDAnimatedImage animatedImageFrameAtIndex:] + 152 (SDAnimatedImage.m:307)
23 MyApp 0x00000001026b2360 __41-[SDImageFramePool prefetchFrameAtIndex:]_block_invoke + 48 (SDImageFramePool.m:119)
24 Foundation 0x00000001a142b2f0 NSBLOCKOPERATION_IS_CALLING_OUT_TO_A_BLOCK + 24 (NSOperation.m:1545)
25 Foundation 0x00000001a13ff4a0 -[NSBlockOperation main] + 104 (NSOperation.m:1564)
26 Foundation 0x00000001a13ff430 NSOPERATION_IS_INVOKING_MAIN + 16 (NSOperation.m:2189)
27 Foundation 0x00000001a13c07d8 -[NSOperation start] + 708 (NSOperation.m:2206)
28 Foundation 0x00000001a13c050c NSOPERATIONQUEUE_IS_STARTING_AN_OPERATION + 16 (NSOperation.m:2220)
29 Foundation 0x00000001a13c5c20 __NSOQSchedule_f + 172 (NSOperation.m:2231)
30 libdispatch.dylib 0x00000001ae585114 _dispatch_block_async_invoke2 + 148 (queue.c:555)
31 libdispatch.dylib 0x00000001ae575fdc _dispatch_client_callout + 20 (object.m:560)
32 libdispatch.dylib 0x00000001ae57946c _dispatch_continuation_pop + 504 (inline_internal.h:2632)
33 libdispatch.dylib 0x00000001ae578b6c _dispatch_async_redirect_invoke + 736 (queue.c:830)
34 libdispatch.dylib 0x00000001ae587a6c _dispatch_root_queue_drain + 396 (inline_internal.h:0)
35 libdispatch.dylib 0x00000001ae588284 _dispatch_worker_thread2 + 164 (queue.c:7052)
36 libsystem_pthread.dylib 0x00000001f3ffbdbc _pthread_wqthread + 228 (pthread.c:2631)
37 libsystem_pthread.dylib 0x00000001f3ffbb98 start_wqthread + 8 (:-1)

Pull Request Description

When the animated image is released, and still call the code may lead to crash:
UIImage *frame = [animatedProvider animatedImageFrameAtIndex:index];

So here adding a code protection。Please let me know if you have any questions.

Summary by CodeRabbit

  • Bug Fixes
    • Improved reliability when handling animated images by ensuring proper validation of the image provider during background operations.

@coderabbitai
Copy link

coderabbitai bot commented May 14, 2025

Walkthrough

The change updates the retrieval of the animatedProvider instance in SDImageFramePool.m so that it occurs inside the background operation block. A nil-check is added to ensure the provider is valid when accessed, returning early if it is nil. No public API or exported declarations are altered.

Changes

File(s) Change Summary
SDWebImage/Private/SDImageFramePool.m Moved retrieval of animatedProvider into the background operation block; added nil-check.

Poem

In the pool where image frames gleam,
A provider now checked in the stream.
No more stale or missing delight,
We fetch it inside, where timing is right.
With a hop and a skip, bugs take flight—
CodeRabbit keeps your images bright! 🐇✨

Tip

⚡️ Faster reviews with caching
  • CodeRabbit now supports caching for code and dependencies, helping speed up reviews. This means quicker feedback, reduced wait times, and a smoother review experience overall. Cached data is encrypted and stored securely. This feature will be automatically enabled for all accounts on May 16th. To opt out, configure Review - Disable Cache at either the organization or repository level. If you prefer to disable all data retention across your organization, simply turn off the Data Retention setting under your Organization Settings.

Enjoy the performance boost—your workflow just got faster.


📜 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 cac9a55 and 88a57ce.

📒 Files selected for processing (1)
  • SDWebImage/Private/SDImageFramePool.m (1 hunks)
⏰ Context from checks skipped due to timeout of 90000ms (5)
  • GitHub Check: Unit Test (visionOS)
  • GitHub Check: Unit Test (iOS)
  • GitHub Check: Unit Test (macOS)
  • GitHub Check: Build Library
  • GitHub Check: Cocoapods Lint
🔇 Additional comments (1)
SDWebImage/Private/SDImageFramePool.m (1)

118-121: Great fix for preventing access to a deallocated provider!

This change correctly addresses the crash described in PR #3813. By moving the retrieval of animatedProvider inside the block and adding a nil check, you ensure that the code doesn't attempt to access a frame from a provider that has been deallocated between the time the operation was scheduled and when it executes.

The previous implementation was vulnerable to accessing a deallocated object because the provider is a weak property that could become nil if the original provider is released. This defensive programming approach prevents the crash in ImageIO/CoreGraphics when animatedImageFrameAtIndex: is called on a released object.


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.

@dreampiggy
Copy link
Contributor

dreampiggy commented May 14, 2025

Does this really help for the OOM issue ? Seems when playing animated image, the self.provider should always be non-nil ?

But anyway, this change is safe and can reduce the lifetime of that SDAnimatedImage object, which may help for RAM usage.

@dreampiggy dreampiggy merged commit 0d6e4fe into SDWebImage:master May 14, 2025
5 of 8 checks passed
@dreampiggy dreampiggy changed the title crashfix: SDImageIOAnimatedCoder crash Reduce memory peak when playing the GIF on SDAnimatedImageView Jun 10, 2025
@dreampiggy dreampiggy added this to the 5.21.1 milestone Jun 10, 2025
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