Skip to content

Commit ca384b8

Browse files
authored
Ticker should dispatch creation and disposal events. (flutter#137844)
1 parent 8969288 commit ca384b8

File tree

2 files changed

+26
-0
lines changed

2 files changed

+26
-0
lines changed

packages/flutter/lib/src/scheduler/ticker.dart

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,15 @@ class Ticker {
7171
_debugCreationStack = StackTrace.current;
7272
return true;
7373
}());
74+
// TODO(polina-c): stop duplicating code across disposables
75+
// https://github.com/flutter/flutter/issues/137435
76+
if (kFlutterMemoryAllocationsEnabled) {
77+
MemoryAllocations.instance.dispatchObjectCreated(
78+
library: 'package:flutter/scheduler.dart',
79+
className: '$Ticker',
80+
object: this,
81+
);
82+
}
7483
}
7584

7685
TickerFuture? _future;
@@ -319,6 +328,12 @@ class Ticker {
319328
/// with a [TickerCanceled] error.
320329
@mustCallSuper
321330
void dispose() {
331+
// TODO(polina-c): stop duplicating code across disposables
332+
// https://github.com/flutter/flutter/issues/137435
333+
if (kFlutterMemoryAllocationsEnabled) {
334+
MemoryAllocations.instance.dispatchObjectDisposed(object: this);
335+
}
336+
322337
if (_future != null) {
323338
final TickerFuture localFuture = _future!;
324339
_future = null;

packages/flutter/test/scheduler/ticker_test.dart

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ void main() {
2323
}
2424

2525
final Ticker ticker = Ticker(handleTick);
26+
addTearDown(ticker.dispose);
2627

2728
expect(ticker.isTicking, isFalse);
2829
expect(ticker.isActive, isFalse);
@@ -100,6 +101,7 @@ void main() {
100101

101102
testWidgetsWithLeakTracking('Ticker control test', (WidgetTester tester) async {
102103
late Ticker ticker;
104+
addTearDown(() => ticker.dispose());
103105

104106
void testFunction() {
105107
ticker = Ticker((Duration _) { });
@@ -154,6 +156,7 @@ void main() {
154156
}
155157

156158
final Ticker ticker = Ticker(handleTick);
159+
addTearDown(ticker.dispose);
157160
ticker.start();
158161

159162
expect(ticker.isTicking, isTrue);
@@ -179,6 +182,7 @@ void main() {
179182
}
180183

181184
final Ticker ticker = Ticker(handleTick);
185+
addTearDown(ticker.dispose);
182186
ticker.start();
183187

184188
expect(tickCount, equals(0));
@@ -198,4 +202,11 @@ void main() {
198202

199203
ticker.stop();
200204
});
205+
206+
test('Ticker dispatches memory events', () async {
207+
await expectLater(
208+
await memoryEvents(() => Ticker((_) {}).dispose(), Ticker,),
209+
areCreateAndDispose,
210+
);
211+
});
201212
}

0 commit comments

Comments
 (0)