iOS Platform View: Fixed overrelease of the observer. (flutter/engine#13093)
This commit is contained in:
@@ -48,6 +48,20 @@ class PlatformViewIOS final : public PlatformView {
|
||||
void SetSemanticsEnabled(bool enabled) override;
|
||||
|
||||
private:
|
||||
/// Smart pointer for use with objective-c observers.
|
||||
/// This guarentees we remove the observer.
|
||||
class ScopedObserver {
|
||||
public:
|
||||
ScopedObserver();
|
||||
~ScopedObserver();
|
||||
void reset(id<NSObject> observer);
|
||||
ScopedObserver(const ScopedObserver&) = delete;
|
||||
ScopedObserver& operator=(const ScopedObserver&) = delete;
|
||||
|
||||
private:
|
||||
id<NSObject> observer_;
|
||||
};
|
||||
|
||||
fml::WeakPtr<FlutterViewController> owner_controller_;
|
||||
// Since the `ios_surface_` is created on the platform thread but
|
||||
// used on the GPU thread we need to protect it with a mutex.
|
||||
@@ -58,7 +72,7 @@ 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_;
|
||||
ScopedObserver dealloc_view_controller_observer_;
|
||||
|
||||
// |PlatformView|
|
||||
void HandlePlatformMessage(fml::RefPtr<flutter::PlatformMessage> message) override;
|
||||
|
||||
@@ -54,15 +54,15 @@ void PlatformViewIOS::SetOwnerViewController(fml::WeakPtr<FlutterViewController>
|
||||
|
||||
// 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();
|
||||
}]);
|
||||
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();
|
||||
}] retain]);
|
||||
|
||||
if (owner_controller_) {
|
||||
ios_surface_ =
|
||||
@@ -174,4 +174,23 @@ void PlatformViewIOS::SetTextInputPlugin(fml::scoped_nsprotocol<FlutterTextInput
|
||||
text_input_plugin_ = plugin;
|
||||
}
|
||||
|
||||
PlatformViewIOS::ScopedObserver::ScopedObserver() : observer_(nil) {}
|
||||
|
||||
PlatformViewIOS::ScopedObserver::~ScopedObserver() {
|
||||
if (observer_) {
|
||||
[[NSNotificationCenter defaultCenter] removeObserver:observer_];
|
||||
[observer_ release];
|
||||
}
|
||||
}
|
||||
|
||||
void PlatformViewIOS::ScopedObserver::reset(id<NSObject> observer) {
|
||||
if (observer != observer_) {
|
||||
if (observer_) {
|
||||
[[NSNotificationCenter defaultCenter] removeObserver:observer_];
|
||||
[observer_ release];
|
||||
}
|
||||
observer_ = observer;
|
||||
}
|
||||
}
|
||||
|
||||
} // namespace flutter
|
||||
|
||||
Reference in New Issue
Block a user