Skip to content

fix: Dispose gesture recognizers in RenderBaseChart - #2114

Open
a1573595 wants to merge 1 commit into
imaNNeo:mainfrom
a1573595:fix/1975-dispose-leaks
Open

fix: Dispose gesture recognizers in RenderBaseChart#2114
a1573595 wants to merge 1 commit into
imaNNeo:mainfrom
a1573595:fix/1975-dispose-leaks

Conversation

@a1573595

Copy link
Copy Markdown

Description

RenderBaseChart.initGestureRecognizers creates a PanGestureRecognizer, a TapGestureRecognizer and a LongPressGestureRecognizer, but nothing ever released them. With the leak_tracker_flutter_testing harness from the issue, line_chart_test.dart alone reports 70 notDisposed leaks for those three types.

Where they are disposed. The issue suggests disposing in detach(). That would leak-check clean but is not safe: a RenderObject can be detached and re-attached (for example when it moves in the tree), and the recognizers must still be usable after that — disposing them in detach() would leave the chart with dead recognizers on re-attach. They are disposed in dispose() 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 TransformationController was constructed inside the test files themselves, not by AxisChartScaffoldWidget or CustomInteractiveViewer — 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 (via addTearDown). 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_testing as a dev dependency plus test/flutter_test_config.dart. It is already a transitive dependency of flutter_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:

flutter test test/chart/line_chart/line_chart_test.dart

Before — main

23 tests pass, then tearDownAll fails with 70 undisposed objects:

notDisposed:
  total: 70
  objects:
    PanGestureRecognizer:
      test: passes canBeScaled true for FlScaleAxis.free
      identityHashCode: 764283347
    TapGestureRecognizer:
      test: passes canBeScaled true for FlScaleAxis.free
      identityHashCode: 453320791
    LongPressGestureRecognizer:
      test: passes canBeScaled true for FlScaleAxis.free
      identityHashCode: 811505688
    ... (69 recognizer entries in total)

Full log: leak-report-before.txt

After — this branch

Same 23 tests, leak-free:

00:01 +23: (tearDownAll)
00:01 +23: All tests passed!

Full log: leak-report-after.txt

Running the whole suite (664 tests) with the same harness is also leak-free.

Checklist

  • I have followed the Contributor Guide when preparing my PR.
  • I have updated/added tests for ALL new/updated/fixed functionality.
  • I have updated/added relevant documentation and added dartdoc comments with ///.
  • I have updated/added relevant examples in example.

Breaking Change?

  • Yes, this PR is a breaking change.
  • No, this PR is not a breaking change.

Related Issues

Closes #1975

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.
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.

Memory leak detection with leak_tracker_flutter_testing

1 participant