Skip to content

Commit 65c71d4

Browse files
authored
Instrument more disposables. (#137309)
1 parent fea5613 commit 65c71d4

File tree

4 files changed

+44
-3
lines changed

4 files changed

+44
-3
lines changed

packages/flutter/lib/src/animation/animation_controller.dart

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -246,7 +246,9 @@ class AnimationController extends Animation<double>
246246
required TickerProvider vsync,
247247
}) : assert(upperBound >= lowerBound),
248248
_direction = _AnimationDirection.forward {
249-
_maybeDispatchObjectCreation();
249+
if (kFlutterMemoryAllocationsEnabled) {
250+
_maybeDispatchObjectCreation();
251+
}
250252
_ticker = vsync.createTicker(_tick);
251253
_internalSetValue(value ?? lowerBound);
252254
}
@@ -278,7 +280,9 @@ class AnimationController extends Animation<double>
278280
}) : lowerBound = double.negativeInfinity,
279281
upperBound = double.infinity,
280282
_direction = _AnimationDirection.forward {
281-
_maybeDispatchObjectCreation();
283+
if (kFlutterMemoryAllocationsEnabled) {
284+
_maybeDispatchObjectCreation();
285+
}
282286
_ticker = vsync.createTicker(_tick);
283287
_internalSetValue(value);
284288
}

packages/flutter/lib/src/widgets/overlay.dart

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@ import 'framework.dart';
1414
import 'lookup_boundary.dart';
1515
import 'ticker_provider.dart';
1616

17+
const String _flutterWidgetsLibrary = 'package:flutter/widgets.dart';
18+
1719
// Examples can assume:
1820
// late BuildContext context;
1921

@@ -81,7 +83,11 @@ class OverlayEntry implements Listenable {
8183
bool opaque = false,
8284
bool maintainState = false,
8385
}) : _opaque = opaque,
84-
_maintainState = maintainState;
86+
_maintainState = maintainState {
87+
if (kFlutterMemoryAllocationsEnabled) {
88+
_maybeDispatchObjectCreation();
89+
}
90+
}
8591

8692
/// This entry will include the widget built by this builder in the overlay at
8793
/// the entry's position.
@@ -140,6 +146,19 @@ class OverlayEntry implements Listenable {
140146
/// The currently mounted `_OverlayEntryWidgetState` built using this [OverlayEntry].
141147
ValueNotifier<_OverlayEntryWidgetState?>? _overlayEntryStateNotifier = ValueNotifier<_OverlayEntryWidgetState?>(null);
142148

149+
// TODO(polina-c): stop duplicating code across disposables
150+
// https://github.com/flutter/flutter/issues/137435
151+
/// Dispatches event of object creation to [MemoryAllocations.instance].
152+
void _maybeDispatchObjectCreation() {
153+
if (kFlutterMemoryAllocationsEnabled) {
154+
MemoryAllocations.instance.dispatchObjectCreated(
155+
library: _flutterWidgetsLibrary,
156+
className: '$OverlayEntry',
157+
object: this,
158+
);
159+
}
160+
}
161+
143162
@override
144163
void addListener(VoidCallback listener) {
145164
assert(!_disposedByOwner);
@@ -216,6 +235,9 @@ class OverlayEntry implements Listenable {
216235
void dispose() {
217236
assert(!_disposedByOwner);
218237
assert(_overlay == null, 'An OverlayEntry must first be removed from the Overlay before dispose is called.');
238+
if (kFlutterMemoryAllocationsEnabled) {
239+
MemoryAllocations.instance.dispatchObjectDisposed(object: this);
240+
}
219241
_disposedByOwner = true;
220242
if (!mounted) {
221243
// If we're still mounted when disposed, then this will be disposed in

packages/flutter/test/flutter_test_config.dart

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,9 @@ Future<void> testExecutable(FutureOr<void> Function() testMain) {
3333
.withTrackedAll()
3434
.withIgnored(
3535
allNotGCed: true,
36+
notDisposed: <String, int?>{
37+
'OverlayEntry': null,
38+
},
3639
);
3740

3841
// Enable golden file testing using Skia Gold.

packages/flutter/test/widgets/overlay_test.dart

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,18 @@ import 'package:leak_tracker_flutter_testing/leak_tracker_flutter_testing.dart';
1010
import 'semantics_tester.dart';
1111

1212
void main() {
13+
test('OverlayEntry dispatches memory events', () async {
14+
await expectLater(
15+
await memoryEvents(
16+
() => OverlayEntry(
17+
builder: (BuildContext context) => Container(),
18+
).dispose(),
19+
OverlayEntry,
20+
),
21+
areCreateAndDispose,
22+
);
23+
});
24+
1325
testWidgetsWithLeakTracking('OverflowEntries context contains Overlay', (WidgetTester tester) async {
1426
final GlobalKey overlayKey = GlobalKey();
1527
bool didBuild = false;

0 commit comments

Comments
 (0)