Skip to content
This repository was archived by the owner on Feb 25, 2025. It is now read-only.

Migrate darwin common "framework_shared" target to ARC #37049

Merged
merged 14 commits into from
Oct 27, 2022

Conversation

cyanglaz
Copy link
Contributor

@cyanglaz cyanglaz commented Oct 26, 2022

The "framework_shared" target was built with arc, there is no blocker to move it to arc besides some standard mrc to arc code changes.

Code changes includes:

  1. Make retain properties to be either "Strong" or "Copy" (I made properties to be copy when I think they should have been copy)
  2. Make non-retain properties "Assign"
  3. Remove all dealloc method, remove all retains call, remove all autorelease call
  4. Make strong instance variables weak before being accessed in the block

This helps the iOS embedder migration work, after this lands, we can now freely move shared code from macOS to "framework_shared"

part of flutter/flutter#112232

Pre-launch Checklist

  • I read the Contributor Guide and followed the process outlined there for submitting PRs.
  • I read the Tree Hygiene wiki page, which explains my responsibilities.
  • I read and followed the Flutter Style Guide and the C++, Objective-C, Java style guides.
  • I listed at least one issue that this PR fixes in the description above.
  • I added new tests to check the change I am making or feature I am adding, or Hixie said the PR is test-exempt. See testing the engine for instructions on
    writing and running engine tests.
  • I updated/added relevant documentation (doc comments with ///).
  • I signed the CLA.
  • All existing and new tests are passing.

If you need help, consider asking for advice on the #hackers-new channel on Discord.

@cyanglaz cyanglaz marked this pull request as ready for review October 26, 2022 20:14
@cyanglaz cyanglaz requested a review from jmagman October 26, 2022 20:14
@@ -230,7 +230,7 @@ FLUTTER_DARWIN_EXPORT
/**
* The method name.
*/
@property(readonly, nonatomic) NSString* method;
@property(readonly, nonatomic, copy) NSString* method;
Copy link
Contributor Author

@cyanglaz cyanglaz Oct 26, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Technically it is a breaking change.

Though I don't think making it a "copy" is going to change the way the public API is actually used unless it was abused where someone intentionally make it mutable?

Thoughts?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since it's readonly, the setter is not synthesized, so it's not a breaking change here.

Copy link
Contributor Author

@cyanglaz cyanglaz Oct 26, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The breaking part I'm talking about is when someone assigns it to a MutableString, then changes the mutableString and this property will be changed.

So it was originally a bug. By "breaking". I meant if someone is abusing this bug to update the value of these properties, their code will stop working.

Copy link
Contributor Author

@cyanglaz cyanglaz Oct 26, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

After another look, I realized I was mixing stuff in my head. I think @hellohuanlin was right. And in addition, since the setters are not synthesized, copy doesn't even make sense here. I will revert these changes.

@cyanglaz cyanglaz requested a review from iskakaushik October 26, 2022 20:58
Copy link
Contributor

@hellohuanlin hellohuanlin left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

overall LGTM

"//flutter/fml",
# "//flutter/runtime",
# "//flutter/shell/common",
# "//third_party/skia",
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

delete these?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

oops, forgot to push.

@@ -108,7 +100,7 @@ - (void)setMessageHandler:(FlutterMessageHandler)handler {
return;
}
// Grab reference to avoid retain on self.
NSObject<FlutterMessageCodec>* codec = _codec;
__weak NSObject<FlutterMessageCodec>* codec = _codec;
FlutterBinaryMessageHandler messageHandler = ^(NSData* message, FlutterBinaryReply callback) {
handler([codec decode:message], ^(id reply) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

maybe just early return if codec is nil? if codec is nil, should this handler be called?

Copy link
Contributor Author

@cyanglaz cyanglaz Oct 26, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I haven't looked into the implementation details. But just looking at this method without context, the behavior is different if we early return when codec is nil. The current behavior is that the handler is always called regardless of the codec's value. So I'd say we should keep it the same behavior and maybe update the code later if we think that's a bug.

Edit: we probably shouldn't even support a nil codec, maybe we can add an FML_DCHECK. But it is probably a good idea doing it in a separate PR.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Agreed there's not real-world case where a nil codec makes sense. Asserting it's not nil sgtm in a followup patch.

@@ -285,7 +255,7 @@ - (void)setMethodCallHandler:(FlutterMethodCallHandler)handler {
return;
}
// Make sure the block captures the codec, not self.
NSObject<FlutterMethodCodec>* codec = _codec;
__weak NSObject<FlutterMethodCodec>* codec = _codec;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

same q here

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is wrong, codec needs to be captured by the block to live longer than the channel object. _codec was actually nil in some scenarios if not captured strongly.

_messenger = [messenger retain];
_codec = [codec retain];
_taskQueue = [taskQueue retain];
_name = name;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Better to copy mutable objects like NSString so if they are really NSMutableString they don't get changed from under you.

Suggested change
_name = name;
_name = [name copy];

Also all the FlutterBasicMessageChannel ivars should be properties... but that doesn't need to be done here.

Comment on lines 130 to 132
_code = code;
_message = message;
_details = details;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

And these properties are copy but you only get that on the property setter, not when you set the synthesized ivar.

Suggested change
_code = code;
_message = message;
_details = details;
_code = [code copy];
_message = [message copy];
_details = [details copy];

}

- (instancetype)initWithMethodName:(NSString*)method arguments:(id)arguments {
NSAssert(method, @"Method name cannot be nil");
self = [super init];
NSAssert(self, @"Super init cannot be nil");
_method = [method retain];
_arguments = [arguments retain];
_method = method;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
_method = method;
_method = [method copy];

_messenger = [messenger retain];
_codec = [codec retain];
_taskQueue = [taskQueue retain];
_name = name;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

etc

@@ -182,18 +170,13 @@ - (instancetype)initWithData:(NSData*)data type:(FlutterStandardDataType)type {
NSAssert(data.length % elementSize == 0, @"Data must contain integral number of elements");
self = [super init];
NSAssert(self, @"Super init cannot be nil");
_data = [data retain];
_data = data;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same idea with NSMutableData

Suggested change
_data = data;
_data = [data copy];

"//third_party/dart/runtime:dart_api",
"//third_party/skia",
]
deps = [ "//flutter/fml" ]
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The other deps aren't necessary?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Correct. They were probably initially necessary but was forgotten to be removed at some point when refactoring the code.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I wonder if some of these were copy-pasted from the macOS embedder initially? I can't imagine why we'd depend on flow, for example, given the current code. Either way, nice catch!

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

They were added 6 years ago so I guess the code structure was a lot different.

Chris Yang added 2 commits October 26, 2022 14:45
@cyanglaz cyanglaz requested a review from jmagman October 26, 2022 21:47
@flutter-dashboard
Copy link

It looks like this pull request may not have tests. Please make sure to add tests before merging. If you need an exemption to this rule, contact Hixie on the #hackers channel in Chat (don't just cc him here, he won't see it! He's on Discord!).

If you are not sure if you need tests, consider this rule of thumb: the purpose of a test is to make sure someone doesn't accidentally revert the fix. Ask yourself, is there anything in your PR that you feel it is important we not accidentally revert back to how it was before your fix?

Reviewers: Read the Tree Hygiene page and make sure this patch meets those guidelines before LGTMing.

@Hixie
Copy link
Contributor

Hixie commented Oct 26, 2022

test-exemption: code refactor with no semantic change

Copy link
Member

@jmagman jmagman left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I see FLUTTER_ASSERT_ARC in a few places, but not many. Should that be added to the .mm files so it's not accidentally built in the future with MRC?

Copy link
Member

@jmagman jmagman left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM, nit about FLUTTER_ASSERT_ARC.

@@ -80,7 +80,7 @@ - (void)testRestorationEnabledWaitsForData {
[restorationPlugin setRestorationData:data];
XCTAssertEqual([capturedResult count], 2u);
XCTAssertEqual([capturedResult objectForKey:@"enabled"], @YES);
XCTAssertEqual([[capturedResult objectForKey:@"data"] data], data);
XCTAssertEqualObjects([[capturedResult objectForKey:@"data"] data], data);
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@goderbauer Could you take a look at the changes I made in this test file to see if they are ok? Did you remember if XCTAssertEqual was intentionally used to compare the address of the 2 data objects, or we just need the content of the data objects to be the same.

Original PR: #23495

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We just need the content of the data objects to be the same.

@cyanglaz cyanglaz requested a review from goderbauer October 27, 2022 16:55
@cyanglaz cyanglaz added the autosubmit Merge PR when tree becomes green via auto submit App label Oct 27, 2022
@auto-submit auto-submit bot merged commit 866b670 into flutter:main Oct 27, 2022
@cyanglaz cyanglaz deleted the darwin_common_arc branch October 27, 2022 20:34
engine-flutter-autoroll added a commit to engine-flutter-autoroll/flutter that referenced this pull request Oct 27, 2022
cyanglaz pushed a commit to cyanglaz/engine that referenced this pull request Oct 28, 2022
engine-flutter-autoroll added a commit to engine-flutter-autoroll/flutter that referenced this pull request Oct 28, 2022
engine-flutter-autoroll added a commit to engine-flutter-autoroll/flutter that referenced this pull request Oct 28, 2022
CaseyHillers pushed a commit to CaseyHillers/engine that referenced this pull request Nov 1, 2022
CaseyHillers pushed a commit that referenced this pull request Nov 1, 2022
…ARC (#37049)" (#37195)

* Revert "Use iOS 16 APIs to rotate orientation (#36874)" (#37165)

* Revert "Migrate darwin common "framework_shared" target to ARC (#37049)"

This reverts commit 866b670.
cyanglaz pushed a commit to cyanglaz/engine that referenced this pull request Nov 1, 2022
@@ -108,7 +102,7 @@ - (void)setMessageHandler:(FlutterMessageHandler)handler {
return;
}
// Grab reference to avoid retain on self.
NSObject<FlutterMessageCodec>* codec = _codec;
__weak NSObject<FlutterMessageCodec>* codec = _codec;
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is wrong, codec needs to be captured by the block to live longer than the channel object.

cyanglaz pushed a commit that referenced this pull request Nov 4, 2022
auto-submit bot pushed a commit that referenced this pull request Nov 4, 2022
cyanglaz pushed a commit to cyanglaz/engine that referenced this pull request Nov 9, 2022
cyanglaz pushed a commit to cyanglaz/engine that referenced this pull request Nov 14, 2022
schwa423 pushed a commit to schwa423/engine that referenced this pull request Nov 16, 2022
cyanglaz pushed a commit to cyanglaz/engine that referenced this pull request Nov 23, 2022
cyanglaz pushed a commit to cyanglaz/engine that referenced this pull request Nov 29, 2022
cyanglaz pushed a commit to cyanglaz/engine that referenced this pull request Dec 13, 2022
auto-submit bot pushed a commit that referenced this pull request Dec 15, 2022
loic-sharma pushed a commit to loic-sharma/flutter-engine that referenced this pull request Dec 16, 2022
loic-sharma pushed a commit to loic-sharma/flutter-engine that referenced this pull request Jan 3, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
autosubmit Merge PR when tree becomes green via auto submit App needs tests platform-ios platform-macos
Projects
None yet
Development

Successfully merging this pull request may close these issues.

6 participants