diff --git a/engine/src/flutter/shell/platform/darwin/ios/framework/Source/FlutterEngineGroup.mm b/engine/src/flutter/shell/platform/darwin/ios/framework/Source/FlutterEngineGroup.mm index fc5beec0c0..b0eca94e44 100644 --- a/engine/src/flutter/shell/platform/darwin/ios/framework/Source/FlutterEngineGroup.mm +++ b/engine/src/flutter/shell/platform/darwin/ios/framework/Source/FlutterEngineGroup.mm @@ -38,8 +38,6 @@ } - (void)dealloc { - NSNotificationCenter* center = [NSNotificationCenter defaultCenter]; - [center removeObserver:self]; [_name release]; [_engines release]; [_project release]; diff --git a/engine/src/flutter/shell/platform/darwin/ios/framework/Source/FlutterMetalLayer.mm b/engine/src/flutter/shell/platform/darwin/ios/framework/Source/FlutterMetalLayer.mm index 83d248f7cd..a343e903ec 100644 --- a/engine/src/flutter/shell/platform/darwin/ios/framework/Source/FlutterMetalLayer.mm +++ b/engine/src/flutter/shell/platform/darwin/ios/framework/Source/FlutterMetalLayer.mm @@ -186,10 +186,6 @@ extern CFTimeInterval display_link_target; return self; } -- (void)dealloc { - [[NSNotificationCenter defaultCenter] removeObserver:self]; -} - - (void)setMaxRefreshRate:(double)refreshRate forceMax:(BOOL)forceMax { // This is copied from vsync_waiter_ios.mm. The vsync waiter has display link scheduled on UI // thread which does not trigger actual core animation frame. As a workaround FlutterMetalLayer diff --git a/engine/src/flutter/shell/platform/darwin/ios/framework/Source/FlutterPluginAppLifeCycleDelegate.mm b/engine/src/flutter/shell/platform/darwin/ios/framework/Source/FlutterPluginAppLifeCycleDelegate.mm index 81c6799f05..11451d38e4 100644 --- a/engine/src/flutter/shell/platform/darwin/ios/framework/Source/FlutterPluginAppLifeCycleDelegate.mm +++ b/engine/src/flutter/shell/platform/darwin/ios/framework/Source/FlutterPluginAppLifeCycleDelegate.mm @@ -31,7 +31,6 @@ static const SEL kSelectorsHandledByPlugins[] = { @end @implementation FlutterPluginAppLifeCycleDelegate { - NSMutableArray* _notificationUnsubscribers; UIBackgroundTaskIdentifier _debugBackgroundTask; // Weak references to registered plugins. @@ -40,16 +39,10 @@ static const SEL kSelectorsHandledByPlugins[] = { - (void)addObserverFor:(NSString*)name selector:(SEL)selector { [[NSNotificationCenter defaultCenter] addObserver:self selector:selector name:name object:nil]; - __block NSObject* blockSelf = self; - dispatch_block_t unsubscribe = ^{ - [[NSNotificationCenter defaultCenter] removeObserver:blockSelf name:name object:nil]; - }; - [_notificationUnsubscribers addObject:[unsubscribe copy]]; } - (instancetype)init { if (self = [super init]) { - _notificationUnsubscribers = [[NSMutableArray alloc] init]; std::string cachePath = fml::paths::JoinPaths({getenv("HOME"), kCallbackCacheSubDir}); [FlutterCallbackCache setCachePath:[NSString stringWithUTF8String:cachePath.c_str()]]; #if not APPLICATION_EXTENSION_API_ONLY @@ -70,12 +63,6 @@ static const SEL kSelectorsHandledByPlugins[] = { return self; } -- (void)dealloc { - for (dispatch_block_t unsubscribe in _notificationUnsubscribers) { - unsubscribe(); - } -} - static BOOL IsPowerOfTwo(NSUInteger x) { return x != 0 && (x & (x - 1)) == 0; } diff --git a/engine/src/flutter/shell/platform/darwin/ios/framework/Source/FlutterPluginAppLifeCycleDelegateTest.mm b/engine/src/flutter/shell/platform/darwin/ios/framework/Source/FlutterPluginAppLifeCycleDelegateTest.mm index 9fcc645575..d96ae4cb90 100644 --- a/engine/src/flutter/shell/platform/darwin/ios/framework/Source/FlutterPluginAppLifeCycleDelegateTest.mm +++ b/engine/src/flutter/shell/platform/darwin/ios/framework/Source/FlutterPluginAppLifeCycleDelegateTest.mm @@ -10,6 +10,12 @@ FLUTTER_ASSERT_ARC +@interface FakePlugin : NSObject +@end + +@implementation FakePlugin +@end + @interface FlutterPluginAppLifeCycleDelegateTest : XCTestCase @end @@ -89,6 +95,21 @@ FLUTTER_ASSERT_ARC [self waitForExpectations:@[ expectation ] timeout:5.0]; OCMVerify([plugin applicationWillTerminate:[UIApplication sharedApplication]]); } + +- (void)testReleasesPluginOnDealloc { + __weak id weakPlugin; + __weak FlutterPluginAppLifeCycleDelegate* weakDelegate; + @autoreleasepool { + FakePlugin* fakePlugin = [[FakePlugin alloc] init]; + weakPlugin = fakePlugin; + FlutterPluginAppLifeCycleDelegate* delegate = [[FlutterPluginAppLifeCycleDelegate alloc] init]; + [delegate addDelegate:fakePlugin]; + weakDelegate = delegate; + } + XCTAssertNil(weakPlugin); + XCTAssertNil(weakDelegate); +} + #endif @end diff --git a/engine/src/flutter/shell/platform/darwin/macos/framework/Source/FlutterAppLifecycleDelegate.mm b/engine/src/flutter/shell/platform/darwin/macos/framework/Source/FlutterAppLifecycleDelegate.mm index ba53217544..e2439b3bd8 100644 --- a/engine/src/flutter/shell/platform/darwin/macos/framework/Source/FlutterAppLifecycleDelegate.mm +++ b/engine/src/flutter/shell/platform/darwin/macos/framework/Source/FlutterAppLifecycleDelegate.mm @@ -13,23 +13,14 @@ #include "flutter/fml/logging.h" #include "flutter/fml/paths.h" -@implementation FlutterAppLifecycleRegistrar { - NSMutableArray* _notificationUnsubscribers; -} +@implementation FlutterAppLifecycleRegistrar - (void)addObserverFor:(NSString*)name selector:(SEL)selector { [[NSNotificationCenter defaultCenter] addObserver:self selector:selector name:name object:nil]; - __block NSObject* blockSelf = self; - dispatch_block_t unsubscribe = ^{ - [[NSNotificationCenter defaultCenter] removeObserver:blockSelf name:name object:nil]; - }; - [_notificationUnsubscribers addObject:[unsubscribe copy]]; } - (instancetype)init { if (self = [super init]) { - _notificationUnsubscribers = [[NSMutableArray alloc] init]; - // Using a macro to avoid errors where the notification doesn't match the // selector. #ifdef OBSERVE_NOTIFICATION @@ -60,15 +51,6 @@ return self; } -- (void)dealloc { - for (dispatch_block_t unsubscribe in _notificationUnsubscribers) { - unsubscribe(); - } - [_notificationUnsubscribers removeAllObjects]; - _delegates = nil; - _notificationUnsubscribers = nil; -} - static BOOL IsPowerOfTwo(NSUInteger x) { return x != 0 && (x & (x - 1)) == 0; } diff --git a/engine/src/flutter/shell/platform/darwin/macos/framework/Source/FlutterAppLifecycleDelegateTest.mm b/engine/src/flutter/shell/platform/darwin/macos/framework/Source/FlutterAppLifecycleDelegateTest.mm index 079ddbb7ff..e073cc3385 100644 --- a/engine/src/flutter/shell/platform/darwin/macos/framework/Source/FlutterAppLifecycleDelegateTest.mm +++ b/engine/src/flutter/shell/platform/darwin/macos/framework/Source/FlutterAppLifecycleDelegateTest.mm @@ -220,4 +220,18 @@ TEST(FlutterAppLifecycleDelegateTest, RespondsToDidChangeOcclusionState) { } } +TEST(FlutterAppLifecycleDelegateTest, ReleasesDelegateOnDealloc) { + __weak FlutterAppLifecycleRegistrar* weakRegistrar; + __weak TestFlutterAppLifecycleDelegate* weakDelegate; + @autoreleasepool { + FlutterAppLifecycleRegistrar* registrar = [[FlutterAppLifecycleRegistrar alloc] init]; + weakRegistrar = registrar; + TestFlutterAppLifecycleDelegate* delegate = [[TestFlutterAppLifecycleDelegate alloc] init]; + weakDelegate = delegate; + [registrar addDelegate:delegate]; + } + EXPECT_EQ(weakRegistrar, nil); + EXPECT_EQ(weakDelegate, nil); +} + } // namespace flutter::testing