stream-chat-flutter icon indicating copy to clipboard operation
stream-chat-flutter copied to clipboard

Crash (ios): high watermark memory limit exceeded

Open nbourdin opened this issue 9 months ago • 10 comments

Which packages are you using?

stream_chat, stream_chat_flutter, stream_chat_flutter_core

On what platforms did you experience the issue?

iOS

What version are you using?

stream_chat_flutter-7.2.2

What happened?

Hello! My app crashes when there are multiple images in the channel GalleryAttachmentBuilder seems to be problem

Relevant log output

[ServicesDaemonManager] interruptionHandler is called. -[FontServicesDaemonManager connection]_block_invoke
* thread #8, name = 'io.flutter.1.ui', stop reason = EXC_RESOURCE (RESOURCE_TYPE_MEMORY: high watermark memory limit exceeded) (limit=1450 MB)
    frame #0: 0x000000010d40248c
->  0x10d40248c: stur   x2, [x0, #0x27]
    0x10d402490: stur   x4, [x0, #0x2f]
    0x10d402494: ret    
    0x10d402498: stp    x29, x30, [x15, #-0x10]!
Target 0: (Runner) stopped.
Lost connection to device.

Flutter doctor output

[!] Flutter (Channel stable, 3.24.5, on macOS 15.4 24E248 darwin-arm64, locale fr-US)
    • Flutter version 3.24.5 on channel stable at /Users/nicolas/flutter
    ! Warning: `dart` on your path resolves to /opt/homebrew/Cellar/dart/3.5.4/libexec/bin/dart, which is not inside your current Flutter SDK checkout at /Users/nicolas/flutter. Consider adding
      /Users/nicolas/flutter/bin to the front of your path.
    • Upstream repository https://github.com/flutter/flutter.git
    • Framework revision dec2ee5c1f (5 months ago), 2024-11-13 11:13:06 -0800
    • Engine revision a18df97ca5
    • Dart version 3.5.4
    • DevTools version 2.37.3
    • If those were intentional, you can disregard the above warnings; however it is recommended to use "git" directly to perform update checks and upgrades.

[✓] Android toolchain - develop for Android devices (Android SDK version 35.0.0)
    • Android SDK at /Users/nicolas/Library/Android/sdk
    • Platform android-35, build-tools 35.0.0
    • ANDROID_HOME = /Users/nicolas/Library/Android/sdk
    • Java binary at: /opt/homebrew/opt/openjdk@17/bin/java
    • Java version OpenJDK Runtime Environment Homebrew (build 17.0.14+0)
    • All Android licenses accepted.

[✓] Xcode - develop for iOS and macOS (Xcode 16.2)
    • Xcode at /Applications/Xcode-16.2.0.app/Contents/Developer
    • Build 16C5032a
    • CocoaPods version 1.16.2

[✓] Chrome - develop for the web
    • Chrome at /Applications/Google Chrome.app/Contents/MacOS/Google Chrome

[!] Android Studio (not installed)
    • Android Studio not found; download from https://developer.android.com/studio/index.html
      (or visit https://flutter.dev/to/macos-android-setup for detailed instructions).

[✓] VS Code (version 1.99.2)
    • VS Code at /Applications/Visual Studio Code.app/Contents
    • Flutter extension version 3.109.20250401

[✓] Connected device (4 available)
...
[✓] Network resources
    • All expected network resources are available.

! Doctor found issues in 2 categories.

Code of Conduct

  • [x] I agree to follow this project's Code of Conduct

nbourdin avatar Apr 22 '25 09:04 nbourdin

@renefloor any idea, what can be the issue here?

xsahil03x avatar Apr 28 '25 11:04 xsahil03x

@nbourdin are these images really large? After (approximately) how many images does the app crash? Is it on specific iOS devices, or the simulator?

renefloor avatar Apr 29 '25 12:04 renefloor

Hello I had to remove about twenty images to prevent my channel from crashing on certain devices (e.g., iPhone 15). However, I’m still experiencing crashes on devices like the iPhone SE. These crashes only occur on physical iPhones, not on the simulator. The images are approximately 2–3 MB each. As for the number of images that cause the app to crash, I can’t say for sure at this point.

nbourdin avatar May 05 '25 12:05 nbourdin

This might be an issue in Flutter itself

xsahil03x avatar May 06 '25 13:05 xsahil03x

Is there a simple way to intercept image loading to convert them to JPEG?

nbourdin avatar May 12 '25 15:05 nbourdin

Ok, i also try the sample app, uploading a lot of heic images and got a crash too

nbourdin avatar May 14 '25 10:05 nbourdin

Hey @nbourdin , Can you also share those images here in order to test? Thanks

xsahil03x avatar May 15 '25 02:05 xsahil03x

i uploaded the same image again and again in the sample app here is the cid : !members-Yhb4mXHsSWaSDbM1qTiJmb5IpcO05ew7JeB_3VVbHic

the crash occurs on iphone SE for exemple when i scroll in the channel

nbourdin avatar May 16 '25 09:05 nbourdin

Hey @nbourdin, I tried scrolling the channel in the iPhone 16 Pro simulator and it didn't crashed. I will check later on a iphone SE to confirm.

https://github.com/user-attachments/assets/0e4a94dc-2c23-4ca6-ab8f-efb2b79bc7c4

xsahil03x avatar May 19 '25 11:05 xsahil03x

yes, this only crashes on certain (physical) models of iphone like SE, iphone 12, etc.

nbourdin avatar May 20 '25 09:05 nbourdin

Hey, have you been able to test with one of these devices?

nbourdin avatar Jun 28 '25 21:06 nbourdin

Hi @nbourdin we've reproduced that there is a memory issue by testing on a physical iPhone SE, but it's hard to debug what exactly is going wrong. We'll try to find the root issue and keep you posted.

renefloor avatar Jul 01 '25 09:07 renefloor

The root cause of this issue is a broken thumbnail mechanism for media attachments. If you inspect the app’s network traffic, images are fetched at their maximum resolution.

Image

Older iOS devices simply can’t handle displaying multiple (e.g., more than 10) high-resolution images due to to implementation of CachedNetworkImage and hardware constraints (limited RAM)

The thumbnail mechanism doesn’t work because the StreamImageAttachmentThumbnail class has a confusing API. It exposes two properties that appear to control the thumbnail size:

  /// Width of the attachment image thumbnail.
  final double? width;

  /// Height of the attachment image thumbnail.
  final double? height;

and

  /// Size of the attachment image thumbnail.
  final Size? thumbnailSize;

In practice, only thumbnailSize is used when constructing the resized image URL:

      if (thumbnailSize != null) {
        imageUrl = imageUrl.getResizedImageUrl(
          width: thumbnailSize.width,
          height: thumbnailSize.height,
          resize: thumbnailResizeType,
          crop: thumbnailCropType,
        );
      }

So when other parts of the code create StreamImageAttachmentThumbnail with only width and height (for example, in GalleryAttachmentBuilder):

                StreamMediaAttachmentThumbnail(
                  media: attachment,
                  width: constraints.maxWidth,
                  height: constraints.maxHeight,
                  fit: BoxFit.cover,
                ),

that’s not enough to make thumbnails work. There are a few other places that need to pass thumbnailSize as well (for example, StreamQuotedMessageWidget). If you update all of these call sites to provide thumbnailSize, the app should no longer crash - even with hundreds of images in a chat. Or at least to make it crash you would need to scroll more extensively and have more images in chat.

Side note 1. When testing thumbnail functionality please delete the app each time as CachedNetworkImage uses a disk cache which may interfere with results.

Side note 1. When calculating thumbnailSize it is necessary to multiply logical points with device pixel ratio obtained via MediaQuery.of(context).devicePixelRatio.

shorbenko avatar Sep 04 '25 11:09 shorbenko

Hey @shorbenko, thanks for digging a bit deeper. We will try and fix this to see if it helps. It might take some time as i am a bit occupied with the FeedsV3 product.

xsahil03x avatar Sep 04 '25 11:09 xsahil03x

Hey @nbourdin , can you try this PR and see if it solves your issue?

xsahil03x avatar Nov 18 '25 00:11 xsahil03x