Baseflow / octo_image

A multifunctional Flutter image widget
https://baseflow.com/
MIT License
156 stars 23 forks source link

Memory leak and crash with gapless #26

Open fryette opened 1 year ago

fryette commented 1 year ago

I got an issue with OOM on a very low device with random crashes. After investigation, I figure out that the problem with ImageHandler was reassigned.

Why did it happen? Because in _OctoImageState in didUpdateWidget method each time reassign previous handler and saves current as well because of placeholder + progressBuilder properties. So in time, you have a situation where during the build method code executes build for all prev handlers and request all the images in the hierarchy(for 12 images on screen after 1 min with 10-sec delay of changing URL have 60 sec/10 delay =6 requests for each image + decoding job which cause the crash).

Fix proposal Instead of each time reassigning handler need to create a new handler for the previous state without a placeholder and progress from the current handle or provide a way how to clear prev handler

instead of _previousHandler = _imageHandler; Need to recreate ImageHandler without placeholder from current ImageHandler

_previousHandler = ImageHandler(
          image: oldWidget.image,
          imageBuilder: oldWidget.imageBuilder,
          placeholderBuilder: oldWidget.placeholderBuilder,
          progressIndicatorBuilder: oldWidget.progressIndicatorBuilder,
          errorBuilder: oldWidget.errorBuilder,
          placeholderFadeInDuration: oldWidget.placeholderFadeInDuration,
          fadeOutDuration: oldWidget.fadeOutDuration,
          fadeOutCurve: oldWidget.fadeOutCurve,
          fadeInDuration: oldWidget.fadeInDuration,
          fadeInCurve: oldWidget.fadeInCurve,
          fit: oldWidget.fit,
          width: oldWidget.width,
          height: oldWidget.height,
          alignment: oldWidget.alignment,
          repeat: oldWidget.repeat,
          color: oldWidget.color,
          colorBlendMode: oldWidget.colorBlendMode,
          matchTextDirection: oldWidget.matchTextDirection,
          filterQuality: oldWidget.filterQuality,
        );

This issue is not related to Flutter 3.x. It's reproducible on 2.10.3 and 3.x.

I also saw a couple of old issues with CachableImage on flutter GitHub, and I think that can be a root cause of crashes

bugrevealingbme commented 1 year ago

I have the annoying problem too. I am using iPhone 13 Pro.

momoDragon commented 1 year ago

Facing a similar issue, app just crashes when loading a few large images in grid view could not find any crash logs on xcode, firebase crashalytics, and user local device app crash logs in user diagnostics device: iphone 13 pro ios16.1.1