From 1be28aa61a4f3551ffb835c2557bca3f167b0b23 Mon Sep 17 00:00:00 2001 From: Valentin Vignal <32538273+ValentinVignal@users.noreply.github.com> Date: Tue, 23 Apr 2024 13:26:09 +0800 Subject: [PATCH] Fix memory leaks in `CupertinoTextMagnifier` (#147208) --- .../flutter/lib/src/cupertino/magnifier.dart | 17 ++++++++++------- .../test/material/selection_area_test.dart | 6 +++++- 2 files changed, 15 insertions(+), 8 deletions(-) diff --git a/packages/flutter/lib/src/cupertino/magnifier.dart b/packages/flutter/lib/src/cupertino/magnifier.dart index f02803b508..fb20960aaa 100644 --- a/packages/flutter/lib/src/cupertino/magnifier.dart +++ b/packages/flutter/lib/src/cupertino/magnifier.dart @@ -82,8 +82,10 @@ class _CupertinoTextMagnifierState extends State // set these values. Offset _currentAdjustedMagnifierPosition = Offset.zero; double _verticalFocalPointAdjustment = 0; - late AnimationController _ioAnimationController; - late Animation _ioAnimation; + late final AnimationController _ioAnimationController; + late final Animation _ioAnimation; + late final CurvedAnimation _ioCurvedAnimation; + @override void initState() { @@ -97,20 +99,21 @@ class _CupertinoTextMagnifierState extends State widget.controller.animationController = _ioAnimationController; widget.magnifierInfo .addListener(_determineMagnifierPositionAndFocalPoint); - + _ioCurvedAnimation = CurvedAnimation( + parent: _ioAnimationController, + curve: widget.animationCurve, + ); _ioAnimation = Tween( begin: 0.0, end: 1.0, - ).animate(CurvedAnimation( - parent: _ioAnimationController, - curve: widget.animationCurve, - )); + ).animate(_ioCurvedAnimation); } @override void dispose() { widget.controller.animationController = null; _ioAnimationController.dispose(); + _ioCurvedAnimation.dispose(); widget.magnifierInfo .removeListener(_determineMagnifierPositionAndFocalPoint); super.dispose(); diff --git a/packages/flutter/test/material/selection_area_test.dart b/packages/flutter/test/material/selection_area_test.dart index 9967576c88..bde06237de 100644 --- a/packages/flutter/test/material/selection_area_test.dart +++ b/packages/flutter/test/material/selection_area_test.dart @@ -9,6 +9,7 @@ import 'package:flutter/material.dart'; import 'package:flutter/rendering.dart'; import 'package:flutter/services.dart'; import 'package:flutter_test/flutter_test.dart'; +import 'package:leak_tracker_flutter_testing/leak_tracker_flutter_testing.dart'; import '../widgets/process_text_utils.dart'; @@ -288,7 +289,10 @@ void main() { expect(content!.plainText, 'How'); }); - testWidgets('stopping drag of end handle will show the toolbar', (WidgetTester tester) async { + testWidgets('stopping drag of end handle will show the toolbar', + // TODO(polina-c): remove when fixed https://github.com/flutter/flutter/issues/145600 [leak-tracking-opt-in] + experimentalLeakTesting: LeakTesting.settings.withTracked(classes: const ['CurvedAnimation']), + (WidgetTester tester) async { final FocusNode focusNode = FocusNode(); addTearDown(focusNode.dispose);