Skip to content

Commit 1be28aa

Browse files
Fix memory leaks in CupertinoTextMagnifier (#147208)
1 parent 3e8408b commit 1be28aa

File tree

2 files changed

+15
-8
lines changed

2 files changed

+15
-8
lines changed

packages/flutter/lib/src/cupertino/magnifier.dart

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -82,8 +82,10 @@ class _CupertinoTextMagnifierState extends State<CupertinoTextMagnifier>
8282
// set these values.
8383
Offset _currentAdjustedMagnifierPosition = Offset.zero;
8484
double _verticalFocalPointAdjustment = 0;
85-
late AnimationController _ioAnimationController;
86-
late Animation<double> _ioAnimation;
85+
late final AnimationController _ioAnimationController;
86+
late final Animation<double> _ioAnimation;
87+
late final CurvedAnimation _ioCurvedAnimation;
88+
8789

8890
@override
8991
void initState() {
@@ -97,20 +99,21 @@ class _CupertinoTextMagnifierState extends State<CupertinoTextMagnifier>
9799
widget.controller.animationController = _ioAnimationController;
98100
widget.magnifierInfo
99101
.addListener(_determineMagnifierPositionAndFocalPoint);
100-
102+
_ioCurvedAnimation = CurvedAnimation(
103+
parent: _ioAnimationController,
104+
curve: widget.animationCurve,
105+
);
101106
_ioAnimation = Tween<double>(
102107
begin: 0.0,
103108
end: 1.0,
104-
).animate(CurvedAnimation(
105-
parent: _ioAnimationController,
106-
curve: widget.animationCurve,
107-
));
109+
).animate(_ioCurvedAnimation);
108110
}
109111

110112
@override
111113
void dispose() {
112114
widget.controller.animationController = null;
113115
_ioAnimationController.dispose();
116+
_ioCurvedAnimation.dispose();
114117
widget.magnifierInfo
115118
.removeListener(_determineMagnifierPositionAndFocalPoint);
116119
super.dispose();

packages/flutter/test/material/selection_area_test.dart

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import 'package:flutter/material.dart';
99
import 'package:flutter/rendering.dart';
1010
import 'package:flutter/services.dart';
1111
import 'package:flutter_test/flutter_test.dart';
12+
import 'package:leak_tracker_flutter_testing/leak_tracker_flutter_testing.dart';
1213

1314
import '../widgets/process_text_utils.dart';
1415

@@ -288,7 +289,10 @@ void main() {
288289
expect(content!.plainText, 'How');
289290
});
290291

291-
testWidgets('stopping drag of end handle will show the toolbar', (WidgetTester tester) async {
292+
testWidgets('stopping drag of end handle will show the toolbar',
293+
// TODO(polina-c): remove when fixed https://github.com/flutter/flutter/issues/145600 [leak-tracking-opt-in]
294+
experimentalLeakTesting: LeakTesting.settings.withTracked(classes: const <String>['CurvedAnimation']),
295+
(WidgetTester tester) async {
292296
final FocusNode focusNode = FocusNode();
293297
addTearDown(focusNode.dispose);
294298

0 commit comments

Comments
 (0)