fix: Dispose gesture recognizers in RenderBaseChart - #2114
Open
a1573595 wants to merge 1 commit into
Open
Conversation
The pan, tap and longPress recognizers created in initGestureRecognizers were never released, which leak_tracker_flutter_testing reports as notDisposed leaks (70 in line_chart_test alone). They are disposed in RenderObject.dispose rather than detach, because a render object may be re-attached after detach and the recognizers must stay usable. The TransformationController leaks from the issue turned out to be test-owned controllers: the library disposes its internal controllers correctly, so the tests now dispose the controllers they create. With the issue's leak_tracker harness enabled locally, the whole suite (664 tests) runs leak-free.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Description
RenderBaseChart.initGestureRecognizerscreates aPanGestureRecognizer, aTapGestureRecognizerand aLongPressGestureRecognizer, but nothing ever released them. With theleak_tracker_flutter_testingharness from the issue,line_chart_test.dartalone reports 70notDisposedleaks for those three types.Where they are disposed. The issue suggests disposing in
detach(). That would leak-check clean but is not safe: aRenderObjectcan be detached and re-attached (for example when it moves in the tree), and the recognizers must still be usable after that — disposing them indetach()would leave the chart with dead recognizers on re-attach. They are disposed indispose()instead, which the framework calls exactly once when the render object is really gone.About the TransformationController leaks. I could not reproduce them as a library bug. Enabling creation stack traces in the leak tracker showed every leaked
TransformationControllerwas constructed inside the test files themselves, not byAxisChartScaffoldWidgetorCustomInteractiveViewer— both of those already dispose the controller they own and correctly leave an externally provided one alone. The fix here is therefore on the test side: the tests now dispose the controllers they create (viaaddTearDown). No library change was needed for that half of the report.With the issue's harness enabled locally, the whole suite (664 tests) runs leak-free.
Test Result
Reproduced with the harness from the issue —
leak_tracker_flutter_testingas a dev dependency plustest/flutter_test_config.dart. It is already a transitive dependency offlutter_test, so no dependency change is needed in this repo; I added it locally only to produce the reports below and reverted it afterwards.Both runs use the exact same command on the same test file:
Before —
main23 tests pass, then
tearDownAllfails with 70 undisposed objects:Full log: leak-report-before.txt
After — this branch
Same 23 tests, leak-free:
Full log: leak-report-after.txt
Running the whole suite (664 tests) with the same harness is also leak-free.
Checklist
///.example.Breaking Change?
Related Issues
Closes #1975