Skip to content

Commit 7d56be4

Browse files
authored
Clean up leak tracker instrumentation tech debt. (#164070)
Fixes flutter/flutter#137435 Tests are not needed because it is refactoring. Requested [exempt](https://discord.com/channels/608014603317936148/608018585025118217/1343801945982505001). @goderbauer , if looks good, can you merge it please, to avoid conflicts, as there are many files here?
1 parent ad87186 commit 7d56be4

34 files changed

+99
-598
lines changed

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

Lines changed: 3 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,6 @@ export 'package:flutter/scheduler.dart' show TickerFuture, TickerProvider;
2323
export 'animation.dart' show Animation, AnimationStatus;
2424
export 'curves.dart' show Curve;
2525

26-
const String _flutterAnimationLibrary = 'package:flutter/animation.dart';
27-
2826
// Examples can assume:
2927
// late AnimationController _controller, fadeAnimationController, sizeAnimationController;
3028
// late bool dismissed;
@@ -255,9 +253,7 @@ class AnimationController extends Animation<double>
255253
required TickerProvider vsync,
256254
}) : assert(upperBound >= lowerBound),
257255
_direction = _AnimationDirection.forward {
258-
if (kFlutterMemoryAllocationsEnabled) {
259-
_maybeDispatchObjectCreation();
260-
}
256+
assert(debugMaybeDispatchCreated('animation', 'AnimationController', this));
261257
_ticker = vsync.createTicker(_tick);
262258
_internalSetValue(value ?? lowerBound);
263259
}
@@ -289,24 +285,11 @@ class AnimationController extends Animation<double>
289285
}) : lowerBound = double.negativeInfinity,
290286
upperBound = double.infinity,
291287
_direction = _AnimationDirection.forward {
292-
if (kFlutterMemoryAllocationsEnabled) {
293-
_maybeDispatchObjectCreation();
294-
}
288+
assert(debugMaybeDispatchCreated('animation', 'AnimationController', this));
295289
_ticker = vsync.createTicker(_tick);
296290
_internalSetValue(value);
297291
}
298292

299-
/// Dispatches event of object creation to [FlutterMemoryAllocations.instance].
300-
void _maybeDispatchObjectCreation() {
301-
if (kFlutterMemoryAllocationsEnabled) {
302-
FlutterMemoryAllocations.instance.dispatchObjectCreated(
303-
library: _flutterAnimationLibrary,
304-
className: '$AnimationController',
305-
object: this,
306-
);
307-
}
308-
}
309-
310293
/// The value at which this animation is deemed to be dismissed.
311294
final double lowerBound;
312295

@@ -946,9 +929,7 @@ class AnimationController extends Animation<double>
946929
}
947930
return true;
948931
}());
949-
if (kFlutterMemoryAllocationsEnabled) {
950-
FlutterMemoryAllocations.instance.dispatchObjectDisposed(object: this);
951-
}
932+
assert(debugMaybeDispatchDisposed(this));
952933
_ticker!.dispose();
953934
_ticker = null;
954935
clearStatusListeners();

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

Lines changed: 2 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -505,15 +505,7 @@ class TrainHoppingAnimation extends Animation<double>
505505
this._nextTrain, {
506506
this.onSwitchedTrain,
507507
}) {
508-
// TODO(polina-c): stop duplicating code across disposables
509-
// https://github.com/flutter/flutter/issues/137435
510-
if (kFlutterMemoryAllocationsEnabled) {
511-
FlutterMemoryAllocations.instance.dispatchObjectCreated(
512-
library: 'package:flutter/animation.dart',
513-
className: '$TrainHoppingAnimation',
514-
object: this,
515-
);
516-
}
508+
assert(debugMaybeDispatchCreated('animation', 'TrainHoppingAnimation', this));
517509
if (_nextTrain != null) {
518510
if (_currentTrain!.value == _nextTrain!.value) {
519511
_currentTrain = _nextTrain;
@@ -598,11 +590,7 @@ class TrainHoppingAnimation extends Animation<double>
598590
/// After this is called, this object is no longer usable.
599591
@override
600592
void dispose() {
601-
// TODO(polina-c): stop duplicating code across disposables
602-
// https://github.com/flutter/flutter/issues/137435
603-
if (kFlutterMemoryAllocationsEnabled) {
604-
FlutterMemoryAllocations.instance.dispatchObjectDisposed(object: this);
605-
}
593+
assert(debugMaybeDispatchDisposed(this));
606594
assert(_currentTrain != null);
607595
_currentTrain!.removeStatusListener(_statusChangeHandler);
608596
_currentTrain!.removeListener(_valueChangeHandler);

packages/flutter/lib/src/foundation/change_notifier.dart

Lines changed: 12 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import 'dart:ui' show VoidCallback;
1111
import 'package:meta/meta.dart';
1212

1313
import 'assertions.dart';
14+
import 'debug.dart';
1415
import 'diagnostics.dart';
1516
import 'memory_allocations.dart';
1617

@@ -100,8 +101,6 @@ abstract class ValueListenable<T> extends Listenable {
100101
T get value;
101102
}
102103

103-
const String _flutterFoundationLibrary = 'package:flutter/foundation.dart';
104-
105104
/// A class that can be extended or mixed in that provides a change notification
106105
/// API using [VoidCallback] for notifications.
107106
///
@@ -156,7 +155,7 @@ mixin class ChangeNotifier implements Listenable {
156155
///
157156
/// As [ChangeNotifier] is used as mixin, it does not have constructor,
158157
/// so we use [addListener] to dispatch the event.
159-
bool _creationDispatched = false;
158+
bool _debugCreationDispatched = false;
160159

161160
/// Used by subclasses to assert that the [ChangeNotifier] has not yet been
162161
/// disposed.
@@ -232,16 +231,13 @@ mixin class ChangeNotifier implements Listenable {
232231
/// so that the method is tree-shaken away when the flag is false.
233232
@protected
234233
static void maybeDispatchObjectCreation(ChangeNotifier object) {
235-
// Tree shaker does not include this method and the class MemoryAllocations
236-
// if kFlutterMemoryAllocationsEnabled is false.
237-
if (kFlutterMemoryAllocationsEnabled && !object._creationDispatched) {
238-
FlutterMemoryAllocations.instance.dispatchObjectCreated(
239-
library: _flutterFoundationLibrary,
240-
className: '$ChangeNotifier',
241-
object: object,
242-
);
243-
object._creationDispatched = true;
244-
}
234+
assert(() {
235+
if (!object._debugCreationDispatched) {
236+
debugMaybeDispatchCreated('foundation', 'ChangeNotifier', object);
237+
object._debugCreationDispatched = true;
238+
}
239+
return true;
240+
}());
245241
}
246242

247243
/// Register a closure to be called when the object changes.
@@ -387,11 +383,11 @@ mixin class ChangeNotifier implements Listenable {
387383
);
388384
assert(() {
389385
_debugDisposed = true;
386+
if (_debugCreationDispatched) {
387+
assert(debugMaybeDispatchDisposed(this));
388+
}
390389
return true;
391390
}());
392-
if (kFlutterMemoryAllocationsEnabled && _creationDispatched) {
393-
FlutterMemoryAllocations.instance.dispatchObjectDisposed(object: this);
394-
}
395391
_listeners = _emptyListeners;
396392
_count = 0;
397393
}

packages/flutter/lib/src/gestures/multidrag.dart

Lines changed: 2 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -39,15 +39,7 @@ abstract class MultiDragPointerState {
3939
/// Creates per-pointer state for a [MultiDragGestureRecognizer].
4040
MultiDragPointerState(this.initialPosition, this.kind, this.gestureSettings)
4141
: _velocityTracker = VelocityTracker.withKind(kind) {
42-
// TODO(polina-c): stop duplicating code across disposables
43-
// https://github.com/flutter/flutter/issues/137435
44-
if (kFlutterMemoryAllocationsEnabled) {
45-
FlutterMemoryAllocations.instance.dispatchObjectCreated(
46-
library: 'package:flutter/gestures.dart',
47-
className: '$MultiDragPointerState',
48-
object: this,
49-
);
50-
}
42+
assert(debugMaybeDispatchCreated('gestures', 'MultiDragPointerState', this));
5143
}
5244

5345
/// Device specific gesture configuration that should be preferred over
@@ -195,11 +187,7 @@ abstract class MultiDragPointerState {
195187
@protected
196188
@mustCallSuper
197189
void dispose() {
198-
// TODO(polina-c): stop duplicating code across disposables
199-
// https://github.com/flutter/flutter/issues/137435
200-
if (kFlutterMemoryAllocationsEnabled) {
201-
FlutterMemoryAllocations.instance.dispatchObjectDisposed(object: this);
202-
}
190+
assert(debugMaybeDispatchDisposed(this));
203191
_arenaEntry?.resolve(GestureDisposition.rejected);
204192
_arenaEntry = null;
205193
assert(() {

packages/flutter/lib/src/gestures/recognizer.dart

Lines changed: 2 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -142,15 +142,7 @@ abstract class GestureRecognizer extends GestureArenaMember with DiagnosticableT
142142
this.supportedDevices,
143143
this.allowedButtonsFilter = _defaultButtonAcceptBehavior,
144144
}) {
145-
// TODO(polina-c): stop duplicating code across disposables
146-
// https://github.com/flutter/flutter/issues/137435
147-
if (kFlutterMemoryAllocationsEnabled) {
148-
FlutterMemoryAllocations.instance.dispatchObjectCreated(
149-
library: 'package:flutter/gestures.dart',
150-
className: '$GestureRecognizer',
151-
object: this,
152-
);
153-
}
145+
assert(debugMaybeDispatchCreated('gestures', 'GestureRecognizer', this));
154146
}
155147

156148
/// The recognizer's owner.
@@ -313,11 +305,7 @@ abstract class GestureRecognizer extends GestureArenaMember with DiagnosticableT
313305
/// GestureDetector widget calls this method).
314306
@mustCallSuper
315307
void dispose() {
316-
// TODO(polina-c): stop duplicating code across disposables
317-
// https://github.com/flutter/flutter/issues/137435
318-
if (kFlutterMemoryAllocationsEnabled) {
319-
FlutterMemoryAllocations.instance.dispatchObjectDisposed(object: this);
320-
}
308+
assert(debugMaybeDispatchDisposed(this));
321309
}
322310

323311
/// Returns a very short pretty description of the gesture that the

packages/flutter/lib/src/material/material.dart

Lines changed: 2 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -677,15 +677,7 @@ abstract class InkFeature {
677677
required this.referenceBox,
678678
this.onRemoved,
679679
}) : _controller = controller as _RenderInkFeatures {
680-
// TODO(polina-c): stop duplicating code across disposables
681-
// https://github.com/flutter/flutter/issues/137435
682-
if (kFlutterMemoryAllocationsEnabled) {
683-
FlutterMemoryAllocations.instance.dispatchObjectCreated(
684-
library: 'package:flutter/material.dart',
685-
className: '$InkFeature',
686-
object: this,
687-
);
688-
}
680+
assert(debugMaybeDispatchCreated('material', 'InkFeature', this));
689681
}
690682

691683
/// The [MaterialInkController] associated with this [InkFeature].
@@ -711,11 +703,7 @@ abstract class InkFeature {
711703
_debugDisposed = true;
712704
return true;
713705
}());
714-
// TODO(polina-c): stop duplicating code across disposables
715-
// https://github.com/flutter/flutter/issues/137435
716-
if (kFlutterMemoryAllocationsEnabled) {
717-
FlutterMemoryAllocations.instance.dispatchObjectDisposed(object: this);
718-
}
706+
assert(debugMaybeDispatchDisposed(this));
719707
_controller._removeFeature(this);
720708
onRemoved?.call();
721709
}

packages/flutter/lib/src/material/mergeable_material.dart

Lines changed: 2 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -146,15 +146,7 @@ class _AnimationTuple {
146146
required this.endAnimation,
147147
required this.gapAnimation,
148148
}) {
149-
// TODO(polina-c): stop duplicating code across disposables
150-
// https://github.com/flutter/flutter/issues/137435
151-
if (kFlutterMemoryAllocationsEnabled) {
152-
FlutterMemoryAllocations.instance.dispatchObjectCreated(
153-
library: 'package:flutter/material.dart',
154-
className: '$_AnimationTuple',
155-
object: this,
156-
);
157-
}
149+
assert(debugMaybeDispatchCreated('material', '_AnimationTuple', this));
158150
}
159151

160152
final AnimationController controller;
@@ -165,9 +157,7 @@ class _AnimationTuple {
165157

166158
@mustCallSuper
167159
void dispose() {
168-
if (kFlutterMemoryAllocationsEnabled) {
169-
FlutterMemoryAllocations.instance.dispatchObjectDisposed(object: this);
170-
}
160+
assert(debugMaybeDispatchDisposed(this));
171161
controller.dispose();
172162
startAnimation.dispose();
173163
endAnimation.dispose();

packages/flutter/lib/src/material/tabs.dart

Lines changed: 2 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -480,15 +480,7 @@ class _IndicatorPainter extends CustomPainter {
480480
required this.indicatorAnimation,
481481
required this.textDirection,
482482
}) : super(repaint: controller.animation) {
483-
// TODO(polina-c): stop duplicating code across disposables
484-
// https://github.com/flutter/flutter/issues/137435
485-
if (kFlutterMemoryAllocationsEnabled) {
486-
FlutterMemoryAllocations.instance.dispatchObjectCreated(
487-
library: 'package:flutter/material.dart',
488-
className: '$_IndicatorPainter',
489-
object: this,
490-
);
491-
}
483+
assert(debugMaybeDispatchCreated('material', '_IndicatorPainter', this));
492484
if (old != null) {
493485
saveTabOffsets(old._currentTabOffsets, old._currentTextDirection);
494486
}
@@ -521,9 +513,7 @@ class _IndicatorPainter extends CustomPainter {
521513
}
522514

523515
void dispose() {
524-
if (kFlutterMemoryAllocationsEnabled) {
525-
FlutterMemoryAllocations.instance.dispatchObjectDisposed(object: this);
526-
}
516+
assert(debugMaybeDispatchDisposed(this));
527517
_painter?.dispose();
528518
}
529519

packages/flutter/lib/src/material/time_picker.dart

Lines changed: 2 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -957,15 +957,7 @@ class _DialPainter extends CustomPainter {
957957
required this.textDirection,
958958
required this.selectedValue,
959959
}) : super(repaint: PaintingBinding.instance.systemFonts) {
960-
// TODO(polina-c): stop duplicating code across disposables
961-
// https://github.com/flutter/flutter/issues/137435
962-
if (kFlutterMemoryAllocationsEnabled) {
963-
FlutterMemoryAllocations.instance.dispatchObjectCreated(
964-
library: 'package:flutter/material.dart',
965-
className: '$_DialPainter',
966-
object: this,
967-
);
968-
}
960+
assert(debugMaybeDispatchCreated('material', '_DialPainter', this));
969961
}
970962

971963
final List<_TappableLabel> primaryLabels;
@@ -982,9 +974,7 @@ class _DialPainter extends CustomPainter {
982974
final int selectedValue;
983975

984976
void dispose() {
985-
if (kFlutterMemoryAllocationsEnabled) {
986-
FlutterMemoryAllocations.instance.dispatchObjectDisposed(object: this);
987-
}
977+
assert(debugMaybeDispatchDisposed(this));
988978
for (final _TappableLabel label in primaryLabels) {
989979
label.painter.dispose();
990980
}

packages/flutter/lib/src/painting/decoration_image.dart

Lines changed: 4 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -319,15 +319,7 @@ abstract interface class DecorationImagePainter {
319319

320320
class _DecorationImagePainter implements DecorationImagePainter {
321321
_DecorationImagePainter._(this._details, this._onChanged) {
322-
// TODO(polina-c): stop duplicating code across disposables
323-
// https://github.com/flutter/flutter/issues/137435
324-
if (kFlutterMemoryAllocationsEnabled) {
325-
FlutterMemoryAllocations.instance.dispatchObjectCreated(
326-
library: 'package:flutter/painting.dart',
327-
className: '$_DecorationImagePainter',
328-
object: this,
329-
);
330-
}
322+
assert(debugMaybeDispatchCreated('painting', '_DecorationImagePainter', this));
331323
}
332324

333325
final DecorationImage _details;
@@ -438,9 +430,7 @@ class _DecorationImagePainter implements DecorationImagePainter {
438430

439431
@override
440432
void dispose() {
441-
if (kFlutterMemoryAllocationsEnabled) {
442-
FlutterMemoryAllocations.instance.dispatchObjectDisposed(object: this);
443-
}
433+
assert(debugMaybeDispatchDisposed(this));
444434
_imageStream?.removeListener(ImageStreamListener(_handleImage, onError: _details.onError));
445435
_image?.dispose();
446436
_image = null;
@@ -862,15 +852,7 @@ class _BlendedDecorationImage implements DecorationImage {
862852

863853
class _BlendedDecorationImagePainter implements DecorationImagePainter {
864854
_BlendedDecorationImagePainter._(this.a, this.b, this.t) {
865-
// TODO(polina-c): stop duplicating code across disposables
866-
// https://github.com/flutter/flutter/issues/137435
867-
if (kFlutterMemoryAllocationsEnabled) {
868-
FlutterMemoryAllocations.instance.dispatchObjectCreated(
869-
library: 'package:flutter/painting.dart',
870-
className: '$_BlendedDecorationImagePainter',
871-
object: this,
872-
);
873-
}
855+
assert(debugMaybeDispatchCreated('painting', '_BlendedDecorationImagePainter', this));
874856
}
875857

876858
final DecorationImagePainter? a;
@@ -901,9 +883,7 @@ class _BlendedDecorationImagePainter implements DecorationImagePainter {
901883

902884
@override
903885
void dispose() {
904-
if (kFlutterMemoryAllocationsEnabled) {
905-
FlutterMemoryAllocations.instance.dispatchObjectDisposed(object: this);
906-
}
886+
assert(debugMaybeDispatchDisposed(this));
907887
a?.dispose();
908888
b?.dispose();
909889
}

0 commit comments

Comments
 (0)