From 99f6a34b0b30d2decf85c862b2efb23005ce4bc8 Mon Sep 17 00:00:00 2001 From: Chris Bracken Date: Fri, 27 Sep 2024 17:17:02 -0700 Subject: [PATCH 1/5] iOS: Migrate FlutterEngineGroup to ARC This migrates FlutterEngineGroup to automatic reference counting. It also migrates `enginesCreatedCount` from an ivar to property syntax. In a followup cleanup, I'll look at migrating engine from `NSMutableArray` to a weak pointer array. Issue: https://github.com/flutter/flutter/issues/137801 --- shell/platform/darwin/ios/BUILD.gn | 2 +- .../framework/Headers/FlutterEngineGroup.h | 2 +- .../framework/Source/FlutterEngineGroup.mm | 41 ++++++------------- 3 files changed, 15 insertions(+), 30 deletions(-) diff --git a/shell/platform/darwin/ios/BUILD.gn b/shell/platform/darwin/ios/BUILD.gn index 090f144cc6eaf..97229ee4d6eea 100644 --- a/shell/platform/darwin/ios/BUILD.gn +++ b/shell/platform/darwin/ios/BUILD.gn @@ -70,6 +70,7 @@ source_set("flutter_framework_source_arc") { "framework/Source/FlutterDartVMServicePublisher.mm", "framework/Source/FlutterEmbedderKeyResponder.h", "framework/Source/FlutterEmbedderKeyResponder.mm", + "framework/Source/FlutterEngineGroup.mm", "framework/Source/FlutterHeadlessDartRunner.mm", "framework/Source/FlutterKeyPrimaryResponder.h", "framework/Source/FlutterKeySecondaryResponder.h", @@ -182,7 +183,6 @@ source_set("flutter_framework_source") { # New files are highly encouraged to be in ARC. # To add new files in ARC, add them to the `flutter_framework_source_arc` target. "framework/Source/FlutterEngine.mm", - "framework/Source/FlutterEngineGroup.mm", "framework/Source/FlutterEngine_Internal.h", "framework/Source/FlutterPlatformPlugin.h", "framework/Source/FlutterPlatformPlugin.mm", diff --git a/shell/platform/darwin/ios/framework/Headers/FlutterEngineGroup.h b/shell/platform/darwin/ios/framework/Headers/FlutterEngineGroup.h index fe77b318fe956..b9dbc9a5eac42 100644 --- a/shell/platform/darwin/ios/framework/Headers/FlutterEngineGroup.h +++ b/shell/platform/darwin/ios/framework/Headers/FlutterEngineGroup.h @@ -38,7 +38,7 @@ FLUTTER_DARWIN_EXPORT /** * Arguments passed as a list of string to Dart's entrypoint function. */ -@property(nonatomic, retain, nullable) NSArray* entrypointArgs; +@property(nonatomic, strong, nullable) NSArray* entrypointArgs; @end /** diff --git a/shell/platform/darwin/ios/framework/Source/FlutterEngineGroup.mm b/shell/platform/darwin/ios/framework/Source/FlutterEngineGroup.mm index b0eca94e44096..3172fc05af9b4 100644 --- a/shell/platform/darwin/ios/framework/Source/FlutterEngineGroup.mm +++ b/shell/platform/darwin/ios/framework/Source/FlutterEngineGroup.mm @@ -5,45 +5,30 @@ #import "flutter/shell/platform/darwin/ios/framework/Headers/FlutterEngineGroup.h" #import "flutter/shell/platform/darwin/ios/framework/Source/FlutterEngine_Internal.h" -@implementation FlutterEngineGroupOptions - -- (void)dealloc { - [_entrypoint release]; - [_libraryURI release]; - [_initialRoute release]; - [_entrypointArgs release]; - [super dealloc]; -} +FLUTTER_ASSERT_ARC +@implementation FlutterEngineGroupOptions @end @interface FlutterEngineGroup () @property(nonatomic, copy) NSString* name; -@property(nonatomic, retain) NSMutableArray* engines; -@property(nonatomic, retain) FlutterDartProject* project; +@property(nonatomic, strong) NSMutableArray* engines; +@property(nonatomic, strong) FlutterDartProject* project; +@property(nonatomic, assign) int enginesCreatedCount; @end -@implementation FlutterEngineGroup { - int _enginesCreatedCount; -} +@implementation FlutterEngineGroup - (instancetype)initWithName:(NSString*)name project:(nullable FlutterDartProject*)project { self = [super init]; if (self) { _name = [name copy]; _engines = [[NSMutableArray alloc] init]; - _project = [project retain]; + _project = project; } return self; } -- (void)dealloc { - [_name release]; - [_engines release]; - [_project release]; - [super dealloc]; -} - - (FlutterEngine*)makeEngineWithEntrypoint:(nullable NSString*)entrypoint libraryURI:(nullable NSString*)libraryURI { return [self makeEngineWithEntrypoint:entrypoint libraryURI:libraryURI initialRoute:nil]; @@ -52,7 +37,7 @@ - (FlutterEngine*)makeEngineWithEntrypoint:(nullable NSString*)entrypoint - (FlutterEngine*)makeEngineWithEntrypoint:(nullable NSString*)entrypoint libraryURI:(nullable NSString*)libraryURI initialRoute:(nullable NSString*)initialRoute { - FlutterEngineGroupOptions* options = [[[FlutterEngineGroupOptions alloc] init] autorelease]; + FlutterEngineGroupOptions* options = [[FlutterEngineGroupOptions alloc] init]; options.entrypoint = entrypoint; options.libraryURI = libraryURI; options.initialRoute = initialRoute; @@ -79,7 +64,7 @@ - (FlutterEngine*)makeEngineWithOptions:(nullable FlutterEngineGroupOptions*)opt initialRoute:initialRoute entrypointArgs:entrypointArgs]; } - [_engines addObject:[NSValue valueWithPointer:engine]]; + [self.engines addObject:[NSValue valueWithPointer:(__bridge void*)engine]]; NSNotificationCenter* center = [NSNotificationCenter defaultCenter]; [center addObserver:self @@ -91,13 +76,13 @@ - (FlutterEngine*)makeEngineWithOptions:(nullable FlutterEngineGroupOptions*)opt } - (FlutterEngine*)makeEngine { - NSString* engineName = [NSString stringWithFormat:@"%@.%d", self.name, ++_enginesCreatedCount]; - FlutterEngine* result = [[FlutterEngine alloc] initWithName:engineName project:self.project]; - return [result autorelease]; + NSString* engineName = + [NSString stringWithFormat:@"%@.%d", self.name, ++self.enginesCreatedCount]; + return [[FlutterEngine alloc] initWithName:engineName project:self.project]; } - (void)onEngineWillBeDealloced:(NSNotification*)notification { - [_engines removeObject:[NSValue valueWithPointer:notification.object]]; + [self.engines removeObject:[NSValue valueWithPointer:(__bridge void*)notification.object]]; } @end From 2f12a4b8758401c6d7003ce32bcdc4537dd0ee7a Mon Sep 17 00:00:00 2001 From: Chris Bracken Date: Sat, 28 Sep 2024 07:36:18 -0700 Subject: [PATCH 2/5] Copy semantics for DartProject property --- .../platform/darwin/ios/framework/Source/FlutterEngineGroup.mm | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/shell/platform/darwin/ios/framework/Source/FlutterEngineGroup.mm b/shell/platform/darwin/ios/framework/Source/FlutterEngineGroup.mm index 3172fc05af9b4..2ce3a3d7479ce 100644 --- a/shell/platform/darwin/ios/framework/Source/FlutterEngineGroup.mm +++ b/shell/platform/darwin/ios/framework/Source/FlutterEngineGroup.mm @@ -13,7 +13,7 @@ @implementation FlutterEngineGroupOptions @interface FlutterEngineGroup () @property(nonatomic, copy) NSString* name; @property(nonatomic, strong) NSMutableArray* engines; -@property(nonatomic, strong) FlutterDartProject* project; +@property(nonatomic, copy) FlutterDartProject* project; @property(nonatomic, assign) int enginesCreatedCount; @end From 675c2b23edf48616b13271d3561dd8fd240095ba Mon Sep 17 00:00:00 2001 From: Chris Bracken Date: Sat, 28 Sep 2024 07:37:46 -0700 Subject: [PATCH 3/5] Make enginesCreatedCount uint --- .../darwin/ios/framework/Source/FlutterEngineGroup.mm | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/shell/platform/darwin/ios/framework/Source/FlutterEngineGroup.mm b/shell/platform/darwin/ios/framework/Source/FlutterEngineGroup.mm index 2ce3a3d7479ce..d1981de0353eb 100644 --- a/shell/platform/darwin/ios/framework/Source/FlutterEngineGroup.mm +++ b/shell/platform/darwin/ios/framework/Source/FlutterEngineGroup.mm @@ -14,7 +14,7 @@ @interface FlutterEngineGroup () @property(nonatomic, copy) NSString* name; @property(nonatomic, strong) NSMutableArray* engines; @property(nonatomic, copy) FlutterDartProject* project; -@property(nonatomic, assign) int enginesCreatedCount; +@property(nonatomic, assign) NSUInteger enginesCreatedCount; @end @implementation FlutterEngineGroup @@ -77,7 +77,7 @@ - (FlutterEngine*)makeEngineWithOptions:(nullable FlutterEngineGroupOptions*)opt - (FlutterEngine*)makeEngine { NSString* engineName = - [NSString stringWithFormat:@"%@.%d", self.name, ++self.enginesCreatedCount]; + [NSString stringWithFormat:@"%@.%lu", self.name, ++self.enginesCreatedCount]; return [[FlutterEngine alloc] initWithName:engineName project:self.project]; } From 08240518945d6d54174da9fad29b08e4bdf584eb Mon Sep 17 00:00:00 2001 From: Chris Bracken Date: Mon, 30 Sep 2024 10:01:57 -0700 Subject: [PATCH 4/5] strong->copy --- .../platform/darwin/ios/framework/Headers/FlutterEngineGroup.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/shell/platform/darwin/ios/framework/Headers/FlutterEngineGroup.h b/shell/platform/darwin/ios/framework/Headers/FlutterEngineGroup.h index b9dbc9a5eac42..47cdc07537fe7 100644 --- a/shell/platform/darwin/ios/framework/Headers/FlutterEngineGroup.h +++ b/shell/platform/darwin/ios/framework/Headers/FlutterEngineGroup.h @@ -38,7 +38,7 @@ FLUTTER_DARWIN_EXPORT /** * Arguments passed as a list of string to Dart's entrypoint function. */ -@property(nonatomic, strong, nullable) NSArray* entrypointArgs; +@property(nonatomic, copy, nullable) NSArray* entrypointArgs; @end /** From 20f12fc07a1c464ac92e2b97556352eaf00fe2ea Mon Sep 17 00:00:00 2001 From: Chris Bracken Date: Mon, 30 Sep 2024 10:14:59 -0700 Subject: [PATCH 5/5] Add bridge TODO --- .../platform/darwin/ios/framework/Source/FlutterEngineGroup.mm | 2 ++ 1 file changed, 2 insertions(+) diff --git a/shell/platform/darwin/ios/framework/Source/FlutterEngineGroup.mm b/shell/platform/darwin/ios/framework/Source/FlutterEngineGroup.mm index d1981de0353eb..b274475f7ed14 100644 --- a/shell/platform/darwin/ios/framework/Source/FlutterEngineGroup.mm +++ b/shell/platform/darwin/ios/framework/Source/FlutterEngineGroup.mm @@ -64,6 +64,7 @@ - (FlutterEngine*)makeEngineWithOptions:(nullable FlutterEngineGroupOptions*)opt initialRoute:initialRoute entrypointArgs:entrypointArgs]; } + // TODO(cbracken): https://github.com/flutter/flutter/issues/155943 [self.engines addObject:[NSValue valueWithPointer:(__bridge void*)engine]]; NSNotificationCenter* center = [NSNotificationCenter defaultCenter]; @@ -82,6 +83,7 @@ - (FlutterEngine*)makeEngine { } - (void)onEngineWillBeDealloced:(NSNotification*)notification { + // TODO(cbracken): https://github.com/flutter/flutter/issues/155943 [self.engines removeObject:[NSValue valueWithPointer:(__bridge void*)notification.object]]; }