Repository: flutter
Issue: Clean up 'not disposed' memory leaks in FF, phase 3.
Labels: c: new feature,P1,a: leak tracking

Looking for volunteers to help with this!!! 🚀
Bigger issue: https://github.com/flutter/flutter/issues/134787

## Goal
 
Clean up remaining memory leaks (~25) in order to switch to leak monitoring state.

## Steps

In general:

1. Leak tracking is disabled by default, but enabled just for three non-blocking windows bots. To enable leak tracking for tests locally, search for `if (isLeakTrackingEnabled()) {` in the code and temporary replace it with `if (true)`. 
2. If you create an issue or PR for cleanups, cc @polina-c 
3. Update everybody on your plans by commenting on this issue, so that we do not duplicate effort.
4. If your PR stops ignoring leaks for some tests, trigger leak verifying bots for your PR: find them in code by `_leak_tracking` and temporarily comment out `bringup: true`. 
5. Ping me on GitHub or twitter (PolinaCC) if a PR is waiting for something more than one business day
6. If your PR or issue is closed, and you noticed I forgot to add label `a: leak tracking`, remind me, please 

Fix a memory leak:

1. Identify a leak from one of the sources: 

    a. [flutter dashboard](https://flutter-dashboard.appspot.com/#/build)
        Look for 3 windows icons on the top left with tooltip ending "leak_tracking". 
        If one of them have the last execution failing, open its `test stdout` and search for `contains leaks:`.

    b. Known not-investigated leaky test
        Search for `[leaks-to-clean]`  in [flutter/test](https://github.com/flutter/flutter/tree/master/packages/flutter/test)

    c. [Not assigned known issue](https://github.com/issues?q=is%3Aopen+is%3Aissue+label%3A%22a%3A+leak+tracking%22+-label%3A%22c%3A+new+feature%22+no%3Aassignee)
    Assign the issue to yourself.

3. Investigate the leak. 

    `if` there is a [known issue](https://github.com/issues?q=is%3Aopen+is%3Aissue+label%3A%22a%3A+leak+tracking%22+-label%3A%22c%3A+new+feature%22+) for the leak and it is assigned
    `then:` add/update TODO for the leak (see template below)
    `else:` (fix the leak) or (create new issue and add the leak TODO to the test)

Or instrument a disposable ([example](https://github.com/flutter/flutter/pull/138804/files)):

- [x] AnimationEagerListenerMixin - no need to instrument
- [x] CurvedAnimation
- [x] TrainHoppingAnimation: https://github.com/flutter/flutter/pull/141635
- [x] ImageInfo: https://github.com/flutter/flutter/pull/141411
- [ ] BoxPainter: https://github.com/flutter/flutter/pull/141526
- [x] ScrollDragController
- [x] SelectionOverlay
- [x] SnapshotPainter
- [x] TextSelectionOverlay

## Templates 

Leak tracking opt-out TODO:

```
testWidgets('<test name>',
// TODO(< polina-c | your alias >): <explanation and link to issue> [leaks-to-clean]
experimentalLeakTesting: LeakTesting.settings.withIgnoredAll(),
(WidgetTester tester) async { ...
```

Leak tracking opt-in TODO (when tracking is opted out in flutter_test_config.dart or in the entire test file):

```
// TODO(polina-c): remove when fixed https://github.com/flutter/flutter/issues/145600 [leak-tracking-opt-in]
experimentalLeakTesting: LeakTesting.settings.withTracked(classes: <String>['CurvedAnimation']),
(WidgetTester tester) async { ...
```

Steps to repro (to be copied to issues):

To repro the leak:
1. Copy link to the issue
2. Search for the copied link in Flutter source code. You will find number of TODOs marked [leaks-to-clean].
3. Remove the opting out line, one of the TODOs
4. Run the test with the TODO, or, if it is global TODO, run all tests, with added --dart-define LEAK_TRACKING=true
5. See the failure.

## References

Issues: [all](https://github.com/issues?q=is%3Aissue+label%3A%22a%3A+leak+tracking%22+), [open](https://github.com/issues?q=is%3Aopen+is%3Aissue+label%3A%22a%3A+leak+tracking%22+-label%3A%22c%3A+new+feature%22+), [known leaks](https://github.com/issues?q=is%3Aopen+is%3Aissue+label%3A%22a%3A+leak+tracking%22+-label%3A%22c%3A+new+feature%22+).
PRs: [all](https://github.com/pulls?q=is%3Apr+label%3A%22a%3A+leak+tracking%22+), [open](https://github.com/pulls?q=is%3Aopen+is%3Apr+archived%3Afalse+label%3A%22a%3A+leak+tracking%22+).

[Discord](https://discord.com/channels/608014603317936148/608018585025118217/1195071058886279228)