Repository navigation
[web] Dispose the flattened picture in Scene.toImage/toImageSync - #194998
Conversation
There was a problem hiding this comment.
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.
| 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; | ||
| } |
There was a problem hiding this comment.
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;
}…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
LayerScene.toImageandLayerScene.toImageSyncflatten the layer tree into a temporaryPicture, 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, everyRenderRepaintBoundary.toImage/toImageSynccall shows up as aCkPicturethat was not disposed.Leak tracking is off for web in our CI (
flutter_test_config.dartreturnsfalseforkIsWeb), so I forced it on for a throwaway branch and ranLinux web_canvaskit_tests_3restricted to the threetestWidgetssuites that calltoImage/toImageSync:CkPicturenot disposed, all three suites fail intearDownAll.This PR disposes the flattened picture as soon as the image has been produced. The new unit test fails without the fix because
Picture.onDisposeis never invoked for the flattened picture.Fixes #189178