Skip to content

Commit

Permalink
iOS: Migrate FlutterEngineGroup to ARC (#55503)
Browse files Browse the repository at this point in the history
This migrates FlutterEngineGroup to automatic reference counting. It also migrates `enginesCreatedCount` from an ivar to property syntax.

I'll look at migrating engine from `NSMutableArray<NSValue*>` to a weak pointer array in a followup patch since it's orthogonal to ARC migration.

Issue: flutter/flutter#137801

[C++, Objective-C, Java style guides]: https://github.com/flutter/engine/blob/main/CONTRIBUTING.md#style
  • Loading branch information
cbracken authored Sep 30, 2024
1 parent def85c7 commit 5e08e4f
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 30 deletions.
2 changes: 1 addition & 1 deletion shell/platform/darwin/ios/BUILD.gn
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down Expand Up @@ -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",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ FLUTTER_DARWIN_EXPORT
/**
* Arguments passed as a list of string to Dart's entrypoint function.
*/
@property(nonatomic, retain, nullable) NSArray<NSString*>* entrypointArgs;
@property(nonatomic, copy, nullable) NSArray<NSString*>* entrypointArgs;
@end

/**
Expand Down
43 changes: 15 additions & 28 deletions shell/platform/darwin/ios/framework/Source/FlutterEngineGroup.mm
Original file line number Diff line number Diff line change
Expand Up @@ -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<NSValue*>* engines;
@property(nonatomic, retain) FlutterDartProject* project;
@property(nonatomic, strong) NSMutableArray<NSValue*>* engines;
@property(nonatomic, copy) FlutterDartProject* project;
@property(nonatomic, assign) NSUInteger 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<NSValue*> 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];
Expand All @@ -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;
Expand All @@ -79,7 +64,8 @@ - (FlutterEngine*)makeEngineWithOptions:(nullable FlutterEngineGroupOptions*)opt
initialRoute:initialRoute
entrypointArgs:entrypointArgs];
}
[_engines addObject:[NSValue valueWithPointer:engine]];
// TODO(cbracken): https://github.com/flutter/flutter/issues/155943
[self.engines addObject:[NSValue valueWithPointer:(__bridge void*)engine]];

NSNotificationCenter* center = [NSNotificationCenter defaultCenter];
[center addObserver:self
Expand All @@ -91,13 +77,14 @@ - (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:@"%@.%lu", self.name, ++self.enginesCreatedCount];
return [[FlutterEngine alloc] initWithName:engineName project:self.project];
}

- (void)onEngineWillBeDealloced:(NSNotification*)notification {
[_engines removeObject:[NSValue valueWithPointer:notification.object]];
// TODO(cbracken): https://github.com/flutter/flutter/issues/155943
[self.engines removeObject:[NSValue valueWithPointer:(__bridge void*)notification.object]];
}

@end

0 comments on commit 5e08e4f

Please sign in to comment.