Skip to content

fix: stop UiImage handing its caller's image handle to the completer - #78

Draft
orestesgaolin wants to merge 1 commit into
fluttercommunity:masterfrom
orestesgaolin:fix/dispose-decoded-blurhash-image
Draft

orestesgaolin wants to merge 1 commit into
fluttercommunity:masterfrom
orestesgaolin:fix/dispose-decoded-blurhash-image

Conversation

@orestesgaolin

Copy link
Copy Markdown

Fixes the Bad state: Cannot clone a disposed image reported in #40, #46 and #74.

Cause

ImageInfo documents that the completer, not the caller, owns the image:

ImageInfo disposes image, even if it is received as a constructor argument. […] It is expected that only one object owns reference to [it]. […] After passing a ui.Image or ImageInfo reference to another object, release the reference.

UiImage._loadAsync puts the caller's own handle into that ImageInfo, and BlurHashState decodes one image, keeps the handle in _image and builds a new UiImage from it on every build. The first time the image cache drops the entry — a platform memory-pressure notification calls PaintingBinding.handleMemoryPressure, which clears it — the completer disposes the state's handle. The next resolve of the same handle calls clone() on it and throws.

That is why the reports say it only happens next to a network image and after a lifecycle change: both are what make the cache evict.

Change

  • UiImage._loadAsync hands over image.clone() and keeps its own handle.
  • BlurHashState owns that handle: it releases the previous image when a newer decode lands, disposes a decode that arrives after the widget is gone, and disposes the current one in dispose(). Without this the clone would turn the crash into a leaked image per mount.
  • BlurHashState.debugDecodedImage (@visibleForTesting) so the disposal is testable.

Test

test/blur_hash_dispose_test.dart keeps a BlurHash mounted, drops the image cache, and changes a MediaQuery dependency so the provider re-resolves — a rotation, a keyboard or a safe-area inset does the same thing in an app.

On master:

00:00 +1 -1: survives the image cache being dropped while mounted [E]
  Expected: empty
    Actual: [
              StateError:Bad state: Cannot clone a disposed image.
            The clone() method of a previously-disposed Image was called. […]
              StateError:Bad state: Cannot clone a disposed image.
            […]
00:00 +1 -1: Some tests failed.

With this change:

00:00 +3: All tests passed!

The second test asserts the decoded image is released on unmount.

On #47

#47 adds an errorBuilder to the same Image. That stops Flutter rethrowing, but the handle is still shared and still disposed, so the placeholder just silently stops rendering. This change removes the cause instead.

🤖 Generated with Claude Code

An ImageStreamCompleter owns and disposes the ImageInfo it is given, but
UiImage._loadAsync wrapped the caller's own ui.Image. BlurHashState decodes
one image, keeps the handle in _image and builds a new UiImage from it on
every build, so the first image cache eviction disposes that handle and the
next resolve throws "Bad state: Cannot clone a disposed image" (fluttercommunity#40, fluttercommunity#46,
fluttercommunity#74).

UiImage now hands over a clone, and BlurHashState owns its handle: it
releases the previous image when a newer decode lands, drops a decode that
arrives after the widget is gone, and disposes the current one on unmount.

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
@orestesgaolin

Copy link
Copy Markdown
Author

Human note: we're testing this fix internally now and will probably roll out to prod to confirm it fixes it in the wild

@orestesgaolin

Copy link
Copy Markdown
Author

Human note 2: I know this PR gives AI slop vibes, but perhaps it may be useful to build proper fix

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.

1 participant