Skip to content

[web] Dispose the flattened picture in Scene.toImage/toImageSync - #194998

Merged
auto-submit[bot] merged 3 commits into
flutter:masterfrom
mdebbar:check-and-fix-issue-189178
Oct 23, 2026
Merged

auto-submit[bot] merged 3 commits into
flutter:masterfrom
mdebbar:check-and-fix-issue-189178

Conversation

@mdebbar

@mdebbar mdebbar commented Oct 21, 2026 •

Copy link
Copy Markdown
Contributor

LayerScene.toImage and LayerScene.toImageSync flatten the layer tree into a temporary Picture, rasterize it, and then drop it on the floor. That picture is never exposed to the caller, so nobody can dispose it, and in any web test that opts into leak tracking, every RenderRepaintBoundary.toImage/toImageSync call shows up as a CkPicture that was not disposed.

Leak tracking is off for web in our CI (flutter_test_config.dart returns false for kIsWeb), so I forced it on for a throwaway branch and ran Linux web_canvaskit_tests_3 restricted to the three testWidgets suites that call toImage/toImageSync:

This PR disposes the flattened picture as soon as the image has been produced. The new unit test fails without the fix because Picture.onDispose is never invoked for the flattened picture.

Fixes #189178

@github-actions github-actions Bot added engine flutter/engine related. See also e: labels. platform-web Web applications specifically team-web Owned by Web platform team labels Oct 21, 2026
@mdebbar mdebbar added the CICD Run CI/CD label Oct 22, 2026
@mdebbar
mdebbar marked this pull request as ready for review October 22, 2026 20:59

@gemini-code-assist gemini-code-assist Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Code Review

This pull request updates the LayerScene class to ensure that the flattened ui.Picture is properly disposed of in both toImage and toImageSync methods by wrapping the operations in try-finally blocks. A regression test was added to verify that the picture is correctly disposed of. The reviewer suggested improving the test implementation by preserving and restoring the original ui.Picture.onCreate and ui.Picture.onDispose callbacks to avoid interfering with global instrumentation.

Comment on lines +199 to +214
ui.Picture.onCreate = created.add;
ui.Picture.onDispose = disposed.add;
try {
final ui.Image syncImage = scene.toImageSync(100, 100);
expect(created, hasLength(1));
expect(disposed, equals(created));
syncImage.dispose();

final ui.Image asyncImage = await scene.toImage(100, 100);
expect(created, hasLength(2));
expect(disposed, equals(created));
asyncImage.dispose();
} finally {
ui.Picture.onCreate = null;
ui.Picture.onDispose = null;
}

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

medium

Overwriting ui.Picture.onCreate and ui.Picture.onDispose directly and then resetting them to null can interfere with global leak tracking or other instrumentation if they are enabled for the test suite.

To prevent silently disabling leak tracking for subsequent tests, we should preserve the existing callbacks, chain them, and restore them in the finally block.

      final void Function(ui.Picture)? oldOnCreate = ui.Picture.onCreate;
      final void Function(ui.Picture)? oldOnDispose = ui.Picture.onDispose;
      ui.Picture.onCreate = (ui.Picture picture) {
        created.add(picture);
        oldOnCreate?.call(picture);
      };
      ui.Picture.onDispose = (ui.Picture picture) {
        disposed.add(picture);
        oldOnDispose?.call(picture);
      };
      try {
        final ui.Image syncImage = scene.toImageSync(100, 100);
        expect(created, hasLength(1));
        expect(disposed, equals(created));
        syncImage.dispose();

        final ui.Image asyncImage = await scene.toImage(100, 100);
        expect(created, hasLength(2));
        expect(disposed, equals(created));
        asyncImage.dispose();
      } finally {
        ui.Picture.onCreate = oldOnCreate;
        ui.Picture.onDispose = oldOnDispose;
      }

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

I agree with this

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Done

harryterkelsen
harryterkelsen previously approved these changes Oct 22, 2026

@harryterkelsen harryterkelsen left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

LGTM

@mdebbar mdebbar added the autosubmit Merge PR when tree becomes green via auto submit App label Oct 22, 2026
@mdebbar mdebbar added autosubmit Merge PR when tree becomes green via auto submit App and removed autosubmit Merge PR when tree becomes green via auto submit App labels Oct 22, 2026
suryaKommana2662 pushed a commit to suryaKommana2662/flutter that referenced this pull request Oct 29, 2026
…lutter#194998)

`LayerScene.toImage` and `LayerScene.toImageSync` flatten the layer tree
into a temporary `Picture`, rasterize it, and then drop it on the floor.
That picture is never exposed to the caller, so nobody can dispose it,
and in any web test that opts into leak tracking, every
`RenderRepaintBoundary.toImage`/`toImageSync` call shows up as a
`CkPicture` that was not disposed.

Leak tracking is off for web in our CI (`flutter_test_config.dart`
returns `false` for `kIsWeb`), so I forced it on for a throwaway branch
and ran `Linux web_canvaskit_tests_3` restricted to the three
`testWidgets` suites that call `toImage`/`toImageSync`:
- Before (master): https://ci.chromium.org/b/8667224650151673425 — 16 ×
`CkPicture` not disposed, all three suites fail in `tearDownAll`.
- After (this PR): https://ci.chromium.org/b/8667215553340059873 — same
43 tests, leak free.

This PR disposes the flattened picture as soon as the image has been
produced. The new unit test fails without the fix because
`Picture.onDispose` is never invoked for the flattened picture.

Fixes flutter#189178
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

CICD Run CI/CD engine flutter/engine related. See also e: labels. platform-web Web applications specifically team-web Owned by Web platform team

Projects

None yet

Development

Successfully merging this pull request may close these issues.

CkPicture not disposed with RenderRepaintBoundary.toImage/Sync on Chrome

2 participants