Repository navigation
fix: Resolve memory leak in image_list example - #189526
Conversation
|
It looks like this pull request may not have tests. Please make sure to add tests or get an explicit test exemption before merging. If you are not sure if you need tests, consider this rule of thumb: the purpose of a test is to make sure someone doesn't accidentally revert the fix. Ask yourself, is there anything in your PR that you feel it is important we not accidentally revert back to how it was before your fix? Reviewers: Read the Tree Hygiene page and make sure this patch meets those guidelines before LGTMing. If you believe this PR qualifies for a test exemption, contact "@test-exemption-reviewer" in the #hackers channel in Discord (don't just cc them here, they won't see it!). The test exemption team is a small volunteer group, so all reviewers should feel empowered to ask for tests, without delegating that responsibility entirely to the test exemption group. |
|
Thanks for your pull request! It looks like this may be your first contribution to a Google open source project. Before we can look at your pull request, you'll need to sign a Contributor License Agreement (CLA). View this failed invocation of the CLA check for more information. For the most up to date status, view the checks section at the bottom of the pull request. |
There was a problem hiding this comment.
Code Review
This pull request refactors the image_list example by renaming variables for style consistency and moving the initialization of AnimationControllers from the build method to a class-level field, adding a dispose method to clean them up. Feedback recommends initializing the controllers in initState instead of using a late final field to prevent unnecessary resource allocation during lazy initialization in dispose, and renaming the field to _controllers to follow Dart private naming conventions.
| late final controllers = <AnimationController>[ | ||
| for (int i = 0; i < imagesCount; i++) | ||
| AnimationController( | ||
| duration: const Duration(milliseconds: 3600), | ||
| vsync: this, | ||
| )..repeat(), | ||
| ]; | ||
|
|
||
| @override | ||
| void dispose() { | ||
| for (final controller in controllers) { | ||
| controller.dispose(); | ||
| } | ||
| super.dispose(); | ||
| } |
There was a problem hiding this comment.
Using a late final field initializer for controllers can lead to unnecessary resource allocation during widget disposal. If the widget is disposed before it is ever built (e.g., in certain test scenarios or rapid navigation), accessing controllers in dispose() will trigger its lazy initialization. This creates and starts 50 AnimationControllers only to immediately dispose of them.
Additionally, to conform to Dart private naming conventions (consistent with _counter), the field should be prefixed with an underscore (_controllers).
It is highly recommended to initialize the controllers in initState() instead.
late final List<AnimationController> _controllers;
@override
void initState() {
super.initState();
_controllers = <AnimationController>[
for (int i = 0; i < imagesCount; i++)
AnimationController(
duration: const Duration(milliseconds: 3600),
vsync: this,
)..repeat(),
];
}
@override
void dispose() {
for (final controller in _controllers) {
controller.dispose();
}
super.dispose();
}References
- Effective Dart: Style recommends prefixing private identifiers with an underscore. (link)
There was a problem hiding this comment.
Can you address this feedback and add a test to validate the memory leak has been fixed?
There was a problem hiding this comment.
Sure. I will do that in a moment.
There was a problem hiding this comment.
Can you address this feedback and add a test to validate the memory leak has been fixed?
Done!. Added the tests to validate that no memory leaks, and also fixed the one line lint warning I missed earlier after running 'flutter analyze'
| final ByteData byteData = await rootBundle.load('images/coast.jpg'); | ||
| httpServer.listen((HttpRequest request) async { | ||
| const chunk_size = 2048; | ||
| const chunkSize = 2048; |
There was a problem hiding this comment.
Can you remove the changes here unrelated to the actual bug fix, like changing variable names?
There was a problem hiding this comment.
Sure, I can do just that.
There was a problem hiding this comment.
Can you remove the changes here unrelated to the actual bug fix, like changing variable names?
Done!
|
It looks like this pull request may not have tests. Please make sure to add tests or get an explicit test exemption before merging. If you are not sure if you need tests, consider this rule of thumb: the purpose of a test is to make sure someone doesn't accidentally revert the fix. Ask yourself, is there anything in your PR that you feel it is important we not accidentally revert back to how it was before your fix? Reviewers: Read the Tree Hygiene page and make sure this patch meets those guidelines before LGTMing. If both you and the PR author believe this PR qualifies for a test exemption, escalate to the tech lead for the changed area to review and apply the |
Piinks
left a comment
There was a problem hiding this comment.
Thanks for addressing the review feedback and adding the widget test! This needs dart format run on examples/image_list/lib/main.dart and examples/image_list/test/widget_test.dart.
|
|
||
| void main() { | ||
| testWidgets('MyHomePage disposes AnimationControllers properly', (WidgetTester tester) async { | ||
| HttpOverrides.global = _FakeHttpOverrides(); |
There was a problem hiding this comment.
Nit: Consider resetting HttpOverrides.global in addTearDown so global state doesn't leak if additional tests are added to this file later
There was a problem hiding this comment.
Nit: Consider resetting
HttpOverrides.globalinaddTearDownso global state doesn't leak if additional tests are added to this file later
And done.
Thanks for the review!
- Moved AnimationController initialization to a late final variable to prevent redundant instantiation. - Added dispose() method to properly clean up the controllers. - Renamed images to imagesCount to conform to Dart naming conventions.
…ir original identifiers.
…ist and fix lint warnings (type of local variable was non-obvious)
da63d9a to
7da0f16
Compare
|
autosubmit label was removed for flutter/flutter/189526, because - The status or check suite Tree_analyze has failed. Please fix the issues identified (or deflake) before re-applying this label. |
|
@Okikday can you address the failing checks here? |
I have pushed a fix for the cross-import check (used widgets.dart instead of Material import and found the button by tooltip). So could you add the CICD label so CI re-runs? Thanks! |
|
Thanks for running the Tree analyzer workflow @AbdeMohlbi |
|
autosubmit label was removed for flutter/flutter/189526, because This PR has not met approval requirements for merging. The PR author is not a member of flutter-hackers and needs 1 more review(s) in order to merge this PR.
|
yuh uh |
This PR resolves a memory leak and cleans up naming conventions in the `image_list` Devicelab benchmark example. **What is changing:** - Moved the `AnimationController` instantiation out of the `build` method and into a `late final` variable to prevent redundant instantiation. - Added a `dispose()` method to properly clean up the controllers. **Why:** Previously, 50 new controllers were being instantiated on every `setState` trigger without ever being disposed, causing a memory leak. This refactor correctly manages the widget lifecycle while preserving the underlying performance benchmark (by appending `..repeat()` to the initialization so the UI thread remains appropriately stressed). *This PR fixes a trivial lifecycle and styling issue in a devicelab test script. No open issue is associated.* ## Pre-launch Checklist - [x] I read the [Contributor Guide] and followed the process outlined there for submitting PRs. - [x] I read the [AI contribution guidelines] and understand my responsibilities, or I am not using AI tools. - [x] I read the [Tree Hygiene] wiki page, which explains my responsibilities. - [x] I read and followed the [Flutter Style Guide], including [Features we expect every widget to implement]. - [x] I signed the [CLA]. - [ ] I listed at least one issue that this PR fixes in the description above. - [ ] I updated/added relevant documentation (doc comments with `///`). - [x] I added new tests to check the change I am making, or this PR is [test-exempt]. - [ ] I followed the [breaking change policy] and added [Data Driven Fixes] where supported. - [x] All existing and new tests are passing. [Contributor Guide]: https://github.com/flutter/flutter/blob/main/docs/contributing/Tree-hygiene.md#overview [AI contribution guidelines]: https://github.com/flutter/flutter/blob/main/docs/contributing/Tree-hygiene.md#ai-contribution-guidelines [Tree Hygiene]: https://github.com/flutter/flutter/blob/main/docs/contributing/Tree-hygiene.md [test-exempt]: https://github.com/flutter/flutter/blob/main/docs/contributing/Tree-hygiene.md#tests [Flutter Style Guide]: https://github.com/flutter/flutter/blob/main/docs/contributing/Style-guide-for-Flutter-repo.md [Features we expect every widget to implement]: https://github.com/flutter/flutter/blob/main/docs/contributing/Style-guide-for-Flutter-repo.md#features-we-expect-every-widget-to-implement [CLA]: https://cla.developers.google.com/ [breaking change policy]: https://github.com/flutter/flutter/blob/main/docs/contributing/Tree-hygiene.md#handling-breaking-changes [Data Driven Fixes]: https://github.com/flutter/flutter/blob/main/docs/contributing/Data-driven-Fixes.md
Roll Flutter from fcfb4f203eae to 934720261be0 (38 revisions) flutter/flutter@fcfb4f2...9347202 2026-11-03 [email protected] Roll Skia from 5bceaa033999 to c733ad3e37cb (5 revisions) (flutter/flutter#195872) 2026-11-03 [email protected] Roll Skia from 34d2bf479385 to 5bceaa033999 (9 revisions) (flutter/flutter#195864) 2026-11-03 [email protected] fix: prefer pointer events over semantics tap for mouse clicks on web (flutter/flutter#191529) 2026-11-03 [email protected] [VPAT] Expose a semantics text field value while it is not being edited (flutter/flutter#195445) 2026-11-03 [email protected] [Impeller] Make a GL context current before constructing an AiksContext without a surface (flutter/flutter#195629) 2026-11-03 [email protected] [flutter_tools] Eliminate globals.dart from project.dart (flutter/flutter#194066) 2026-11-03 [email protected] [tool] Complete context runner elimination and migrate AppContext usages to ToolContext (flutter/flutter#190797) 2026-11-03 [email protected] [web] Unify font collections between CanvasKit and Skwasm (flutter/flutter#195539) 2026-11-03 [email protected] [Android] Replay predictive back state for retained engines (flutter/flutter#192286) 2026-11-03 [email protected] [vulkan]: Adds depth/stencil synchronization for subpasses (flutter/flutter#195689) 2026-11-03 [email protected] [web] Honor requested upscaling in instantiateImageCodecWithSize and ImageDescriptor.instantiateCodec (flutter/flutter#195836) 2026-11-03 [email protected] Roll Skia from 866b61ca92c9 to 34d2bf479385 (9 revisions) (flutter/flutter#195848) 2026-11-03 [email protected] Roll Fuchsia Test Scripts from xl1LWBuppzu-BnOzV... to B6zarykZp6PKWRueg... (flutter/flutter#195847) 2026-11-03 [email protected] Remove unused ffi (flutter/flutter#195710) 2026-11-02 [email protected] Revert: [Android] Let the raster thread use every core of a chip without little cores (flutter/flutter#195838) 2026-11-02 [email protected] Run 10 impeller iOS benchmarks as one devicelab task (flutter/flutter#195661) 2026-11-02 [email protected] Use named uniforms instead of setFloat in StretchEffect (flutter/flutter#195372) 2026-11-02 [email protected] [flutter_tools] Report when the browser disconnects during a web test run (flutter/flutter#192198) 2026-11-02 [email protected] fix: Resolve memory leak in image_list example (flutter/flutter#189526) 2026-11-02 [email protected] Only run the common local engine configurations in presubmit (flutter/flutter#195688) 2026-11-02 [email protected] Update gcloud_version to version [email protected] (flutter/flutter#195769) 2026-11-02 [email protected] [Impeller] Order the Vulkan copy-texture-to-texture layout transition after the copy (flutter/flutter#195814) 2026-11-02 [email protected] [Impeller] Order the Vulkan ResizeTexture layout transition after its blit (flutter/flutter#195721) 2026-11-02 [email protected] [web] Fix CanvasKit fragment shader uniforms being freed or mutated after recording (flutter/flutter#195718) 2026-11-02 [email protected] Roll Fuchsia Linux SDK from MqypI6RNyVZ_FLUE-... to T_-6-6X1lDMghhAvX... (flutter/flutter#195809) 2026-11-02 [email protected] [Android] Log why Impeller falls back to OpenGLES (flutter/flutter#194764) 2026-11-02 [email protected] Roll Skia from 3f35fb6e591b to 866b61ca92c9 (11 revisions) (flutter/flutter#195802) 2026-11-02 [email protected] Roll Skia from 3f35fb6e591b to 866b61ca92c9 (11 revisions) (flutter/flutter#195802) 2026-11-02 [email protected] Only run the common local engine configurations in presubmit (flutter/flutter#195688) 2026-11-02 [email protected] Balance tool_integration_tests subshards by expected duration (flutter/flutter#195628) 2026-11-02 [email protected] [web_paragraph] Map code units without a cluster to a cluster (flutter/flutter#195384) 2026-11-02 [email protected] [web] Hide synthetic autofill inputs from assistive technology (flutter/flutter#195618) 2026-11-02 [email protected] Roll Packages from 3742a4e to 3f34a6f (8 revisions) (flutter/flutter#195795) 2026-11-02 [email protected] [web] Unskip web tests that pass on CanvasKit and Skwasm: platform_view_test, route_notification_messages_test (flutter/flutter#195605) 2026-11-02 [email protected] Roll Skia from e5ad3868b8db to 3f35fb6e591b (1 revision) (flutter/flutter#195789) 2026-11-02 [email protected] Roll Skia from f5215e74aa16 to e5ad3868b8db (3 revisions) (flutter/flutter#195779) 2026-11-02 [email protected] Roll Skia from e43478a2f9f8 to f5215e74aa16 (1 revision) (flutter/flutter#195776) 2026-11-01 [email protected] Roll Fuchsia Linux SDK from kW0A0xxNkqf38ljm4... to MqypI6RNyVZ_FLUE-... (flutter/flutter#195768) If this roll has caused a breakage, revert this CL and set the roller to dry run mode using the controls here: https://autoroll.skia.org/r/flutter-packages Please CC [email protected],[email protected] on the revert to ensure that a human is aware of the problem. To file a bug in Packages: https://github.com/flutter/flutter/issues/new/choose ...
This PR resolves a memory leak and cleans up naming conventions in the
image_listDevicelab benchmark example.What is changing:
AnimationControllerinstantiation out of thebuildmethod and into alate finalvariable to prevent redundant instantiation.dispose()method to properly clean up the controllers.Why:
Previously, 50 new controllers were being instantiated on every
setStatetrigger without ever being disposed, causing a memory leak. This refactor correctly manages the widget lifecycle while preserving the underlying performance benchmark (by appending..repeat()to the initialization so the UI thread remains appropriately stressed).This PR fixes a trivial lifecycle and styling issue in a devicelab test script. No open issue is associated.
Pre-launch Checklist
///).