Create helper method to dispatch object creation and disposal. (#163637)

Contributes to https://github.com/flutter/flutter/issues/137435.

Will test cover and convert to 'ready for review' after getting
agreement about the refactoring.
This commit is contained in:
Polina Cherkasova 2025-02-19 17:57:31 -08:00 committed by GitHub
parent 7251a0645c
commit 01fe4e262a
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 90 additions and 14 deletions

View File

@ -379,15 +379,9 @@ class ReverseAnimation extends Animation<double>
class CurvedAnimation extends Animation<double> with AnimationWithParentMixin<double> {
/// Creates a curved animation.
CurvedAnimation({required this.parent, required this.curve, this.reverseCurve}) {
// TODO(polina-c): stop duplicating code across disposables
// https://github.com/flutter/flutter/issues/137435
if (kFlutterMemoryAllocationsEnabled) {
FlutterMemoryAllocations.instance.dispatchObjectCreated(
library: 'package:flutter/animation.dart',
className: '$CurvedAnimation',
object: this,
);
}
assert(
debugMaybeDispatchObjectCreated('package:flutter/animation.dart', 'CurvedAnimation', this),
);
_updateCurveDirection(parent.status);
parent.addStatusListener(_updateCurveDirection);
}
@ -434,11 +428,7 @@ class CurvedAnimation extends Animation<double> with AnimationWithParentMixin<do
/// Cleans up any listeners added by this CurvedAnimation.
void dispose() {
// TODO(polina-c): stop duplicating code across disposables
// https://github.com/flutter/flutter/issues/137435
if (kFlutterMemoryAllocationsEnabled) {
FlutterMemoryAllocations.instance.dispatchObjectDisposed(object: this);
}
assert(debugMaybeDispatchObjectDisposed(this));
isDisposed = true;
parent.removeStatusListener(_updateCurveDirection);
}

View File

@ -12,6 +12,7 @@ library;
import 'dart:ui' as ui show Brightness;
import 'assertions.dart';
import 'memory_allocations.dart';
import 'platform.dart';
import 'print.dart';
@ -132,3 +133,35 @@ String? activeDevToolsServerAddress;
/// The uri for the connected vm service protocol.
String? connectedVmServiceUri;
/// If memory allocation tracking is enabled, dispatch object creation.
///
/// This method is not member of FlutterMemoryAllocations, because
/// [FlutterMemoryAllocations] should not increase size of the Flutter application
/// if memory allocations are disabled.
///
/// Should be called only from within an assert.
///
/// Returns true to make it easier to be wrapped into `assert`.
bool debugMaybeDispatchObjectCreated(String library, String className, Object object) {
if (kFlutterMemoryAllocationsEnabled) {
FlutterMemoryAllocations.instance.dispatchObjectCreated(
library: library,
className: className,
object: object,
);
}
return true;
}
/// If memory allocations tracking is enabled, dispatch object disposal.
///
/// Should be called only from within an assert.
///
/// Returns true to make it easier to be wrapped into `assert`.
bool debugMaybeDispatchObjectDisposed(Object object) {
if (kFlutterMemoryAllocationsEnabled) {
FlutterMemoryAllocations.instance.dispatchObjectDisposed(object: object);
}
return true;
}

View File

@ -47,4 +47,57 @@ void main() {
expect(printBuffer.toString(), matches(r'^Action "throws" took .+'));
});
});
group('Memory allocations', () {
ObjectEvent? dispatchedEvent;
final Object object = List<int>.filled(1, 0);
void listener(ObjectEvent event) {
expect(dispatchedEvent, null);
dispatchedEvent = event;
}
setUp(() {
dispatchedEvent = null;
FlutterMemoryAllocations.instance.addListener(listener);
});
tearDown(() {
FlutterMemoryAllocations.instance.removeListener(listener);
});
test('debugMaybeDispatchObjectCreated', () async {
debugMaybeDispatchObjectCreated('library', 'class', object);
if (kFlutterMemoryAllocationsEnabled) {
final ObjectEvent? theEvent = dispatchedEvent;
if (theEvent is! ObjectCreated) {
fail('Expected ObjectCreated event');
}
expect(theEvent.object, object);
expect(theEvent.library, 'library');
expect(theEvent.className, 'class');
} else {
expect(dispatchedEvent, isNull);
}
});
test('debugMaybeDispatchObjectDisposed', () async {
debugMaybeDispatchObjectDisposed(object);
if (kFlutterMemoryAllocationsEnabled) {
final ObjectEvent? theEvent = dispatchedEvent;
if (theEvent is! ObjectDisposed) {
fail('Expected ObjectDisposed event');
}
expect(theEvent.object, object);
} else {
expect(dispatchedEvent, isNull);
}
});
});
}