From f4bfc0c5f7fdb045c45c53920dcaf634e9e8b34b Mon Sep 17 00:00:00 2001 From: xiaoxiaowesley <44207000+xiaoxiaowesley@users.noreply.github.com> Date: Fri, 6 Aug 2021 11:30:02 +0800 Subject: [PATCH] fix crash SemanticsObject dealloc and access the children (flutter/engine#27786) --- .../darwin/ios/framework/Source/SemanticsObject.mm | 10 ++++++++++ .../ios/framework/Source/SemanticsObjectTest.mm | 14 ++++++++++++++ 2 files changed, 24 insertions(+) diff --git a/engine/src/flutter/shell/platform/darwin/ios/framework/Source/SemanticsObject.mm b/engine/src/flutter/shell/platform/darwin/ios/framework/Source/SemanticsObject.mm index 76e5145443..a2ff91ff5c 100644 --- a/engine/src/flutter/shell/platform/darwin/ios/framework/Source/SemanticsObject.mm +++ b/engine/src/flutter/shell/platform/darwin/ios/framework/Source/SemanticsObject.mm @@ -372,6 +372,7 @@ CGRect ConvertRectToGlobal(SemanticsObject* reference, CGRect local_rect) { @implementation SemanticsObject { fml::scoped_nsobject _container; NSMutableArray* _children; + BOOL _inDealloc; } #pragma mark - Override base class designated initializers @@ -413,6 +414,7 @@ CGRect ConvertRectToGlobal(SemanticsObject* reference, CGRect local_rect) { _parent = nil; _container.get().semanticsObject = nil; [_platformViewSemanticsContainer release]; + _inDealloc = YES; [super dealloc]; } @@ -686,6 +688,14 @@ CGRect ConvertRectToGlobal(SemanticsObject* reference, CGRect local_rect) { } - (id)accessibilityContainer { + if (_inDealloc) { + // In iOS9, `accessibilityContainer` will be called by `[UIAccessibilityElementSuperCategory + // dealloc]` during `[super dealloc]`. And will crash when accessing `_children` which has + // called `[_children release]` in `[SemanticsObject dealloc]`. + // https://github.com/flutter/flutter/issues/87247 + return nil; + } + if (![self isAccessibilityBridgeAlive]) { return nil; } diff --git a/engine/src/flutter/shell/platform/darwin/ios/framework/Source/SemanticsObjectTest.mm b/engine/src/flutter/shell/platform/darwin/ios/framework/Source/SemanticsObjectTest.mm index 77e0edfc73..f6475beff2 100644 --- a/engine/src/flutter/shell/platform/darwin/ios/framework/Source/SemanticsObjectTest.mm +++ b/engine/src/flutter/shell/platform/darwin/ios/framework/Source/SemanticsObjectTest.mm @@ -644,6 +644,20 @@ class MockAccessibilityBridge : public AccessibilityBridgeIos { XCTAssertEqual(object.accessibilityValue, nativeSwitch.accessibilityValue); } +- (void)testSemanticsObjectDeallocated { + fml::WeakPtrFactory factory( + new flutter::MockAccessibilityBridge()); + fml::WeakPtr bridge = factory.GetWeakPtr(); + SemanticsObject* parent = [[SemanticsObject alloc] initWithBridge:bridge uid:0]; + SemanticsObject* child = [[SemanticsObject alloc] initWithBridge:bridge uid:1]; + parent.children = @[ child ]; + // Validate SemanticsObject deallocation does not crash. + // https://github.com/flutter/flutter/issues/66032 + __weak SemanticsObject* weakObject = parent; + parent = nil; + XCTAssertNil(weakObject); +} + - (void)testFlutterSemanticsObjectReturnsNilContainerWhenBridgeIsNotAlive { FlutterSemanticsObject* parentObject; FlutterSemanticsObject* delegate;