From 65c71d4294bd64375f9bb3441d805ab7a2a6b0e7 Mon Sep 17 00:00:00 2001 From: Polina Cherkasova Date: Fri, 27 Oct 2023 21:30:04 -0700 Subject: [PATCH] Instrument more disposables. (#137309) --- .../src/animation/animation_controller.dart | 8 +++++-- packages/flutter/lib/src/widgets/overlay.dart | 24 ++++++++++++++++++- .../flutter/test/flutter_test_config.dart | 3 +++ .../flutter/test/widgets/overlay_test.dart | 12 ++++++++++ 4 files changed, 44 insertions(+), 3 deletions(-) diff --git a/packages/flutter/lib/src/animation/animation_controller.dart b/packages/flutter/lib/src/animation/animation_controller.dart index dbc71d712c..fe886d9a59 100644 --- a/packages/flutter/lib/src/animation/animation_controller.dart +++ b/packages/flutter/lib/src/animation/animation_controller.dart @@ -246,7 +246,9 @@ class AnimationController extends Animation required TickerProvider vsync, }) : assert(upperBound >= lowerBound), _direction = _AnimationDirection.forward { - _maybeDispatchObjectCreation(); + if (kFlutterMemoryAllocationsEnabled) { + _maybeDispatchObjectCreation(); + } _ticker = vsync.createTicker(_tick); _internalSetValue(value ?? lowerBound); } @@ -278,7 +280,9 @@ class AnimationController extends Animation }) : lowerBound = double.negativeInfinity, upperBound = double.infinity, _direction = _AnimationDirection.forward { - _maybeDispatchObjectCreation(); + if (kFlutterMemoryAllocationsEnabled) { + _maybeDispatchObjectCreation(); + } _ticker = vsync.createTicker(_tick); _internalSetValue(value); } diff --git a/packages/flutter/lib/src/widgets/overlay.dart b/packages/flutter/lib/src/widgets/overlay.dart index 6521faab72..cdb7177c4c 100644 --- a/packages/flutter/lib/src/widgets/overlay.dart +++ b/packages/flutter/lib/src/widgets/overlay.dart @@ -14,6 +14,8 @@ import 'framework.dart'; import 'lookup_boundary.dart'; import 'ticker_provider.dart'; +const String _flutterWidgetsLibrary = 'package:flutter/widgets.dart'; + // Examples can assume: // late BuildContext context; @@ -81,7 +83,11 @@ class OverlayEntry implements Listenable { bool opaque = false, bool maintainState = false, }) : _opaque = opaque, - _maintainState = maintainState; + _maintainState = maintainState { + if (kFlutterMemoryAllocationsEnabled) { + _maybeDispatchObjectCreation(); + } + } /// This entry will include the widget built by this builder in the overlay at /// the entry's position. @@ -140,6 +146,19 @@ class OverlayEntry implements Listenable { /// The currently mounted `_OverlayEntryWidgetState` built using this [OverlayEntry]. ValueNotifier<_OverlayEntryWidgetState?>? _overlayEntryStateNotifier = ValueNotifier<_OverlayEntryWidgetState?>(null); + // TODO(polina-c): stop duplicating code across disposables + // https://github.com/flutter/flutter/issues/137435 + /// Dispatches event of object creation to [MemoryAllocations.instance]. + void _maybeDispatchObjectCreation() { + if (kFlutterMemoryAllocationsEnabled) { + MemoryAllocations.instance.dispatchObjectCreated( + library: _flutterWidgetsLibrary, + className: '$OverlayEntry', + object: this, + ); + } + } + @override void addListener(VoidCallback listener) { assert(!_disposedByOwner); @@ -216,6 +235,9 @@ class OverlayEntry implements Listenable { void dispose() { assert(!_disposedByOwner); assert(_overlay == null, 'An OverlayEntry must first be removed from the Overlay before dispose is called.'); + if (kFlutterMemoryAllocationsEnabled) { + MemoryAllocations.instance.dispatchObjectDisposed(object: this); + } _disposedByOwner = true; if (!mounted) { // If we're still mounted when disposed, then this will be disposed in diff --git a/packages/flutter/test/flutter_test_config.dart b/packages/flutter/test/flutter_test_config.dart index c3338ac720..a521e347fd 100644 --- a/packages/flutter/test/flutter_test_config.dart +++ b/packages/flutter/test/flutter_test_config.dart @@ -33,6 +33,9 @@ Future testExecutable(FutureOr Function() testMain) { .withTrackedAll() .withIgnored( allNotGCed: true, + notDisposed: { + 'OverlayEntry': null, + }, ); // Enable golden file testing using Skia Gold. diff --git a/packages/flutter/test/widgets/overlay_test.dart b/packages/flutter/test/widgets/overlay_test.dart index 76dbf6d726..0218b4d905 100644 --- a/packages/flutter/test/widgets/overlay_test.dart +++ b/packages/flutter/test/widgets/overlay_test.dart @@ -10,6 +10,18 @@ import 'package:leak_tracker_flutter_testing/leak_tracker_flutter_testing.dart'; import 'semantics_tester.dart'; void main() { + test('OverlayEntry dispatches memory events', () async { + await expectLater( + await memoryEvents( + () => OverlayEntry( + builder: (BuildContext context) => Container(), + ).dispose(), + OverlayEntry, + ), + areCreateAndDispose, + ); + }); + testWidgetsWithLeakTracking('OverflowEntries context contains Overlay', (WidgetTester tester) async { final GlobalKey overlayKey = GlobalKey(); bool didBuild = false;