From c0b21b16ef84cad520af81bf3431a928a05f3335 Mon Sep 17 00:00:00 2001 From: Kostia Sokolovskyi Date: Mon, 6 Nov 2023 23:14:42 +0100 Subject: [PATCH] SemanticsHandle should dispatch creation and disposal events. (#137960) --- packages/flutter/lib/src/rendering/object.dart | 16 ++++++++++++++++ .../flutter/lib/src/semantics/binding.dart | 18 +++++++++++++++++- .../test/semantics/semantics_binding_test.dart | 10 ++++++++++ 3 files changed, 43 insertions(+), 1 deletion(-) diff --git a/packages/flutter/lib/src/rendering/object.dart b/packages/flutter/lib/src/rendering/object.dart index ff3665735d..50ea05e645 100644 --- a/packages/flutter/lib/src/rendering/object.dart +++ b/packages/flutter/lib/src/rendering/object.dart @@ -822,6 +822,16 @@ typedef LayoutCallback = void Function(T constraints); class _LocalSemanticsHandle implements SemanticsHandle { _LocalSemanticsHandle._(PipelineOwner owner, this.listener) : _owner = owner { + // TODO(polina-c): stop duplicating code across disposables + // https://github.com/flutter/flutter/issues/137435 + if (kFlutterMemoryAllocationsEnabled) { + MemoryAllocations.instance.dispatchObjectCreated( + library: 'package:flutter/rendering.dart', + className: '$_LocalSemanticsHandle', + object: this, + ); + } + if (listener != null) { _owner.semanticsOwner!.addListener(listener!); } @@ -834,6 +844,12 @@ class _LocalSemanticsHandle implements SemanticsHandle { @override void dispose() { + // TODO(polina-c): stop duplicating code across disposables + // https://github.com/flutter/flutter/issues/137435 + if (kFlutterMemoryAllocationsEnabled) { + MemoryAllocations.instance.dispatchObjectDisposed(object: this); + } + if (listener != null) { _owner.semanticsOwner!.removeListener(listener!); } diff --git a/packages/flutter/lib/src/semantics/binding.dart b/packages/flutter/lib/src/semantics/binding.dart index c5d0a40eb5..9f1353900b 100644 --- a/packages/flutter/lib/src/semantics/binding.dart +++ b/packages/flutter/lib/src/semantics/binding.dart @@ -191,7 +191,17 @@ mixin SemanticsBinding on BindingBase { /// /// To obtain a [SemanticsHandle], call [SemanticsBinding.ensureSemantics]. class SemanticsHandle { - SemanticsHandle._(this._onDispose); + SemanticsHandle._(this._onDispose) { + // TODO(polina-c): stop duplicating code across disposables + // https://github.com/flutter/flutter/issues/137435 + if (kFlutterMemoryAllocationsEnabled) { + MemoryAllocations.instance.dispatchObjectCreated( + library: 'package:flutter/semantics.dart', + className: '$SemanticsHandle', + object: this, + ); + } + } final VoidCallback _onDispose; @@ -201,6 +211,12 @@ class SemanticsHandle { /// framework will stop generating semantics information. @mustCallSuper void dispose() { + // TODO(polina-c): stop duplicating code across disposables + // https://github.com/flutter/flutter/issues/137435 + if (kFlutterMemoryAllocationsEnabled) { + MemoryAllocations.instance.dispatchObjectDisposed(object: this); + } + _onDispose(); } } diff --git a/packages/flutter/test/semantics/semantics_binding_test.dart b/packages/flutter/test/semantics/semantics_binding_test.dart index 380713692c..3e24cb78a9 100644 --- a/packages/flutter/test/semantics/semantics_binding_test.dart +++ b/packages/flutter/test/semantics/semantics_binding_test.dart @@ -82,4 +82,14 @@ void main() { expect(SemanticsBinding.instance.semanticsEnabled, isFalse); expect(tester.binding.pipelineOwner.semanticsOwner, isNull); }, semanticsEnabled: false); + + test('SemanticsHandle dispatches memory events', () async { + await expectLater( + await memoryEvents( + () => SemanticsBinding.instance.ensureSemantics().dispose(), + SemanticsHandle, + ), + areCreateAndDispose, + ); + }); }