Skip to content
This repository was archived by the owner on Feb 25, 2025. It is now read-only.
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion shell/platform/darwin/ios/platform_view_ios.h
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,8 @@ class PlatformViewIOS final : public PlatformView {

// |PlatformView|
void SetSemanticsEnabled(bool enabled) override;

void OnFlutterViewControllerDealloced();

private:
fml::WeakPtr<FlutterViewController> owner_controller_;
Expand All @@ -58,7 +60,6 @@ class PlatformViewIOS final : public PlatformView {
std::unique_ptr<AccessibilityBridge> accessibility_bridge_;
fml::scoped_nsprotocol<FlutterTextInputPlugin*> text_input_plugin_;
fml::closure firstFrameCallback_;
fml::scoped_nsprotocol<NSObject*> dealloc_view_controller_observer_;

// |PlatformView|
void HandlePlatformMessage(fml::RefPtr<flutter::PlatformMessage> message) override;
Expand Down
35 changes: 25 additions & 10 deletions shell/platform/darwin/ios/platform_view_ios.mm
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,10 @@

namespace flutter {

static void notificationHandler(CFNotificationCenterRef center, void *observer, CFStringRef name, const void *object, CFDictionaryRef userInfo) {
(static_cast<PlatformViewIOS *>(observer))->OnFlutterViewControllerDealloced();
}

PlatformViewIOS::PlatformViewIOS(PlatformView::Delegate& delegate,
flutter::TaskRunners task_runners)
: PlatformView(delegate, std::move(task_runners)) {
Expand All @@ -27,7 +31,13 @@
#endif // !TARGET_IPHONE_SIMULATOR
}

PlatformViewIOS::~PlatformViewIOS() = default;
PlatformViewIOS::~PlatformViewIOS() {
CFNotificationCenterRemoveEveryObserver
(
CFNotificationCenterGetLocalCenter(),
this
);
};

PlatformMessageRouter& PlatformViewIOS::GetPlatformMessageRouter() {
return platform_message_router_;
Expand All @@ -54,15 +64,15 @@

// Add an observer that will clear out the owner_controller_ ivar and
// the accessibility_bridge_ in case the view controller is deleted.
dealloc_view_controller_observer_.reset([[NSNotificationCenter defaultCenter]
addObserverForName:FlutterViewControllerWillDealloc
object:owner_controller_.get()
queue:[NSOperationQueue mainQueue]
usingBlock:^(NSNotification* note) {
// Implicit copy of 'this' is fine.
accessibility_bridge_.reset();
owner_controller_.reset();
}]);
CFNotificationCenterAddObserver
(
CFNotificationCenterGetLocalCenter(),
this,
&notificationHandler,
(__bridge CFStringRef)FlutterViewControllerWillDealloc,
owner_controller_.get(),
CFNotificationSuspensionBehaviorDeliverImmediately
);

if (owner_controller_) {
ios_surface_ =
Expand All @@ -81,6 +91,11 @@ new AccessibilityBridge(static_cast<FlutterView*>(owner_controller_.get().view),
}
}

void PlatformViewIOS::OnFlutterViewControllerDealloced() {
accessibility_bridge_.reset();
owner_controller_.reset();
}

PointerDataDispatcherMaker PlatformViewIOS::GetDispatcherMaker() {
return [](DefaultPointerDataDispatcher::Delegate& delegate) {
return std::make_unique<SmoothPointerDataDispatcher>(delegate);
Expand Down