diff --git a/packages/flutter/lib/src/semantics/semantics.dart b/packages/flutter/lib/src/semantics/semantics.dart index 48f5f735bc..a3a29f1ca1 100644 --- a/packages/flutter/lib/src/semantics/semantics.dart +++ b/packages/flutter/lib/src/semantics/semantics.dart @@ -3281,7 +3281,17 @@ class SemanticsOwner extends ChangeNotifier { /// Creates a [SemanticsOwner] that manages zero or more [SemanticsNode] objects. SemanticsOwner({ required this.onSemanticsUpdate, - }); + }){ + // 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: '$SemanticsOwner', + object: this, + ); + } + } /// The [onSemanticsUpdate] callback is expected to dispatch [SemanticsUpdate]s /// to the [FlutterView] that is associated with this [PipelineOwner] and/or @@ -3302,6 +3312,9 @@ class SemanticsOwner extends ChangeNotifier { @override void dispose() { + if (kFlutterMemoryAllocationsEnabled) { + MemoryAllocations.instance.dispatchObjectDisposed(object: this); + } _dirtyNodes.clear(); _nodes.clear(); _detachedNodes.clear(); diff --git a/packages/flutter/test/semantics/semantics_test.dart b/packages/flutter/test/semantics/semantics_test.dart index 914bfd1805..a452fea0a6 100644 --- a/packages/flutter/test/semantics/semantics_test.dart +++ b/packages/flutter/test/semantics/semantics_test.dart @@ -6,6 +6,7 @@ import 'dart:ui'; import 'package:flutter/rendering.dart'; import 'package:flutter_test/flutter_test.dart'; +import 'package:leak_tracker_flutter_testing/leak_tracker_flutter_testing.dart'; import 'package:vector_math/vector_math_64.dart'; import '../rendering/rendering_tester.dart'; @@ -965,6 +966,16 @@ void main() { expect(config.onTap, same(onTap)); expect(config.customSemanticsActions[customAction], same(onCustomAction)); }); + + test('SemanticsOwner dispatches memory events', () async { + await expectLater( + await memoryEvents(() => SemanticsOwner( + onSemanticsUpdate: (SemanticsUpdate update) {}, + ).dispose(), SemanticsOwner), + areCreateAndDispose, + ); + }); + } class TestRender extends RenderProxyBox {