Memory leak detection with leak_tracker_flutter_testing
- Dominant language
- Dart
- Stars
- 7.6k
- Forks
- 2k
- Avg merge
- 9d 1h
- Merged PRs (30d)
- 2
Description
**Describe the bug**
After adding the `leak_tracker_flutter_testing` dependency to the dev dependencies to detect memory leaks while running the tests. I found two memory leaks that happens multiple times in different tests. They are not disposed objects as listed below:
- **PanGestureRecognizer** object in `render_base_chart.dart` file
- **TapGestureRecognizer** object in `render_base_chart.dart` file
- **LongPressGestureRecognizer** object in `render_base_chart.dart` file
The above memory leaks can be fixed easily just by disposing them in the `detach` method like below:
```dart
@override
void detach() {
_validForMouseTracker = false;
panGestureRecognizer.dispose();
tapGestureRecognizer.dispose();
longPressGestureRecognizer.dispose();
super.detach();
}
```
Furthermore, it has some other memory leaks, which are hard for me to fix as I'm not very familiar with the source code architecture.
- **TransformationController** in `axis_chart_scaffold_widget.dart` file.
- **TransformationController** in `custom_interactive_viewer.dart` file.
**To Reproduce**
- Add `leak_tracker_flutter_testing` to the dev dependancies
```
dev_dependencies:
...
leak_tracker_flutter_testing: any
```
- Create `flutter_test_config.dart` file in the root of the test folder
```
import 'dart:async';
import 'package:leak_tracker_flutter_testing/leak_tracker_flutter_testing.dart';
Future testExecutable(FutureOr Function() testMain) async {
LeakTesting.enable();
LeakTesting.settings = LeakTesting.settings
.withIgnored(createdByTestHelpers: true);
await testMain();
}
```
- run tests to see detected memory leaks.
**Screenshots**
If applicable, add screenshots, or videoshots to help explain your problem.
**Versions**
- Flutter Version: 3.35.1
- FlChart Version: 1.0.0
I want to create a pull request to fix those memory leaks, but I can't fix the `TransformationController` leaks unless you help me.
Contributor guide
Research direction
Reproduce the leaks with leak_tracker_flutter_testing using the provided flutter_test_config.dart setup, then inspect render_base_chart.dart, axis_chart_scaffold_widget.dart, and custom_interactive_viewer.dart. Verify that the listed gesture recognizers and transformation controllers are cleaned up through their widget lifecycles, and rerun the tests to confirm the reported leaks no longer appear.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- dart
- Domain
- mobile-dev
- Issue type
- Bug
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 45/100