Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

☂️ Migrate iOS embedder objective-c code to ARC (Auto Reference Counting) #137801

Open
1 of 5 tasks
Tracked by #112232
cyanglaz opened this issue Nov 2, 2023 · 4 comments
Open
1 of 5 tasks
Tracked by #112232
Assignees
Labels
engine flutter/engine repository. See also e: labels. P2 Important issues not at the top of the work list platform-ios iOS applications specifically team-ios Owned by iOS platform team triaged-ios Triaged by iOS platform team

Comments

@cyanglaz
Copy link
Contributor

cyanglaz commented Nov 2, 2023

Most of the files in ios Embedder and some shared darwin codes use MRC(Manual Reference Counting).

Manual Reference Counting is easy to make memory management mistake with. Objecgtive-C with Manual Reference Counting is not pleasant to work with, often scaring away contributors.

Migrate all the objective-C code to ARC will boosts Flutter's stability on iOS and making contributing to Flutter iOS embedder more efficient.

This is an umbrella issue tracks all the work needs to be done to migrate the iOS embedder to ARC:

  1. Temporarily enable Objective-C smart pointers (scoped_nsobject, scoped_block etc) to work with both ARC and MRC.
  2. Migrate all Objective-C files to ARC.
  3. iOS: Ensure all delegates use weak pointers and eliminate manual nilling of pointers in [FlutterEngine/FlutterViewController dealloc] #156222
  4. [iOS] Eliminate ARC __bridge casts where possible #155943
  5. Remove the support of Objective-C smart pointers
@cyanglaz cyanglaz added platform-ios iOS applications specifically engine flutter/engine repository. See also e: labels. P1 High-priority issues at the top of the work list fyi-engine For the attention of Engine team team-ios Owned by iOS platform team labels Nov 2, 2023
@chinmaygarde
Copy link
Member

Is there a general issue for this that include macOS?

@chinmaygarde chinmaygarde added the triaged-engine Triaged by Engine team label Nov 6, 2023
@cyanglaz
Copy link
Contributor Author

cyanglaz commented Nov 6, 2023

Is there a general issue for this that include macOS?

macOS is already in ARC so I didn't create an issue for macOS. I can't think of any impact it could cause on macOS. Loop in @cbracken for might insights.

@vashworth vashworth added the triaged-ios Triaged by iOS platform team label Nov 6, 2023
@cbracken
Copy link
Member

cbracken commented Nov 7, 2023

Correct -- we're already building with ARC for macOS. I can't remember if the shared code in '//flutter/platform/darwin/common' is built with ARC enabled or not.

@cbracken
Copy link
Member

cbracken commented Nov 7, 2023

To answer my own question, the bulk of the common code is ARC-enabled.
https://github.com/flutter/engine/blob/cc925b0021330759e18960e1ccbd7e55dec3c375/shell/platform/darwin/common/BUILD.gn#L61-L63

@flutter-triage-bot flutter-triage-bot bot removed fyi-engine For the attention of Engine team triaged-engine Triaged by Engine team labels Nov 7, 2023
auto-submit bot pushed a commit to flutter/engine that referenced this issue Nov 15, 2023
Introduce weak_nsobject from chromium. 

There are some usages of weak_ptr wrapping Objective-C ids, weak_ptr is not really designed for ids and such usages are blocking the arc migration. 

This PR mostly copies the weak_nsobject from chromium, at the same hash that we copied the ARC/MRC compatible scoped_nsobject: https://chromium.googlesource.com/chromium/src/+/fd625125b8a6a3aceaf09993a5f74cfe5368b17f

To match how we used weak_ptr for those ids, I made some changes to the weak_nsobject:
- WeakNSObjects needs to be generated by a WeakNSObjectFactory. The WeakNSObjectFactory is owned by the objc class and acts as the generator of the WeakNSObjects. All the WeakNSObjects' derefing thread should be the same of the WeakNSObjectFactory's creation thread.
- chromuim's WeakNSObjects can be detached from the thread and re-attached to a new thread. To match our weak_ptr behavior, I changed WeakNSObjects to be only accessed from a single thread, the same as weak_ptr

This PR also moves the FlutterEngine to use WeakNSObject and updated related classes.

part of flutter/flutter#137801

[C++, Objective-C, Java style guides]: https://github.com/flutter/engine/blob/main/CONTRIBUTING.md#style
auto-submit bot pushed a commit to flutter/engine that referenced this issue Nov 17, 2023
Move all tests in flutter_test_ios_mrc to arc

Part of flutter/flutter#137801

Changes mostly involves trivial mrc to arc changes. non-trivial changes are commented inline. 

[C++, Objective-C, Java style guides]: https://github.com/flutter/engine/blob/main/CONTRIBUTING.md#style
@jmagman jmagman added P2 Important issues not at the top of the work list and removed P1 High-priority issues at the top of the work list labels Jan 9, 2024
auto-submit bot pushed a commit to flutter/engine that referenced this issue Apr 10, 2024
…rScreenAndSceneIfLoaded to ARC (#51984)

Smart pointers support ARC as of #47612, and the unit tests were migrated in #48162.

Migrate `FlutterRestorationPlugin`, `FlutterTextureRegistryRelay`, and `UIViewController+FlutterScreenAndSceneIfLoaded` from MRC to ARC.  These files do not themselves import any MRC files, making them leaf nodes in the dependency graph of MRC files.  

Doing a few at a time to make the dependency graph manageable, and to easily revert if this causes retain cycles or other memory management issues.

Part of flutter/flutter#137801.
auto-submit bot pushed a commit to flutter/engine that referenced this issue Apr 11, 2024
Framework template updated in flutter/flutter#146481.  See [gen_keycodes README](https://github.com/flutter/flutter/tree/master/dev/tools/gen_keycodes ) for details.

Fixes flutter/flutter#146480 `-Wobjc-redundant-literal-use` error.

Note `-Wobjc-redundant-literal-use` is already on for clang-tidy 
https://github.com/flutter/engine/blob/6dc91bff96a56513a57ed5dd036fb16d25c945fd/.clang-tidy#L13 but in this case it's only true triggered when the file is compiled with ARC.  When I migrated this file to ARC as part of flutter/flutter#137801, it triggered the error.
auto-submit bot pushed a commit to flutter/engine that referenced this issue Apr 12, 2024
Smart pointers support ARC as of #47612, and the unit tests were migrated in #48162.

Migrate `FlutterEmbedderKeyResponder` from MRC to ARC.  Clean up some header imports which made this seem like it depended on not-yet-migrated MRC files.

Part of flutter/flutter#137801.
cbracken added a commit to cbracken/flutter_engine that referenced this issue Sep 27, 2024
Migrates the FlutterAppDelegate.mm translation unit to be compiled with
the `-fobjc-arc` compiler flag.

Issue: flutter/flutter#137801
auto-submit bot pushed a commit to flutter/engine that referenced this issue Sep 27, 2024
Migrates the FlutterAppDelegate.mm translation unit to be compiled with the `-fobjc-arc` compiler flag.

No test changes since no this change includes no semantic changes, and thus covered by existing tests such as [`testReleasesWindowOnDealloc`](https://github.com/flutter/engine/blob/3dfb4622de88abb48ce65be56ff29422ab43805f/shell/platform/darwin/ios/framework/Source/FlutterAppDelegateTest.mm#L139-L153).

Issue: flutter/flutter#137801

[C++, Objective-C, Java style guides]: https://github.com/flutter/engine/blob/main/CONTRIBUTING.md#style
cbracken added a commit to cbracken/flutter_engine that referenced this issue Sep 27, 2024
This is a subclass of FlutterEngine, which is still manually reference
counted, but that's not problematic since the headers have been migrated
to ARC-safe syntax that preserves existing behaviour.

Issue: flutter/flutter#137801
cbracken added a commit to cbracken/flutter_engine that referenced this issue Sep 28, 2024
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<NSValue*>` to a weak pointer array.

Issue: flutter/flutter#137801
auto-submit bot pushed a commit to flutter/engine that referenced this issue Sep 30, 2024
This is a subclass of FlutterEngine, which is still manually reference counted, but that's not problematic since the headers have been migrated to ARC-safe syntax that preserves existing behaviour.

No test changes since this patch makes no semantic changes.

Issue: flutter/flutter#137801

[C++, Objective-C, Java style guides]: https://github.com/flutter/engine/blob/main/CONTRIBUTING.md#style
cbracken added a commit to cbracken/flutter_engine that referenced this issue Sep 30, 2024
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<NSValue*>` to a weak pointer array.

Issue: flutter/flutter#137801
auto-submit bot pushed a commit to flutter/engine that referenced this issue Sep 30, 2024
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
cbracken added a commit to cbracken/flutter_engine that referenced this issue Oct 1, 2024
Keeps fml::scoped_* wrappers used in C++ class declarations in headers,
since those are injected into both ARC and non-ARC translation units.
Once we've migrated all users of those headers to ARC, we can use the
underlying classes directly.

Issue: flutter/flutter#137801
cbracken added a commit to cbracken/flutter_engine that referenced this issue Oct 1, 2024
Keeps fml::scoped_* wrappers used in C++ class declarations in headers,
since those are injected into both ARC and non-ARC translation units.
Once we've migrated all users of those headers to ARC, we can use the
underlying classes directly.

Issue: flutter/flutter#137801
cbracken added a commit to cbracken/flutter_engine that referenced this issue Oct 1, 2024
Migrates from manual reference counting to automatic reference counting.

Issue: flutter/flutter#137801
cbracken added a commit to cbracken/flutter_engine that referenced this issue Oct 1, 2024
Migrates from manual reference counting to automatic reference counting.

Issue: flutter/flutter#137801
auto-submit bot pushed a commit to flutter/engine that referenced this issue Oct 1, 2024
Migrates from manual reference counting to automatic reference counting. Migrates ivars to property syntax. Makes a minor update to a doc comment.

No semantic changes, therefore not changes to tests.

Issue: flutter/flutter#137801

[C++, Objective-C, Java style guides]: https://github.com/flutter/engine/blob/main/CONTRIBUTING.md#style
auto-submit bot pushed a commit to flutter/engine that referenced this issue Oct 2, 2024
Keeps fml::scoped_* wrappers used in C++ class declarations in headers, since those are injected into both ARC and non-ARC translation units and these classes are ARC-safe. Once we've migrated all users of those headers to ARC, we can use the underlying classes directly.

No semantic changes, therefore not changes to tests.

Issue: flutter/flutter#137801

[C++, Objective-C, Java style guides]: https://github.com/flutter/engine/blob/main/CONTRIBUTING.md#style
cbracken added a commit to cbracken/flutter_engine that referenced this issue Oct 2, 2024
Migrates `FlutterEnging` from manual reference counting to ARC. Migrates
properties from `retain` to strong and `assign` to `weak` (where
referencing an Obj-C object).

No semantic changes, therefore no changes to tests.

Issue: flutter/flutter#137801
cbracken added a commit to cbracken/flutter_engine that referenced this issue Oct 2, 2024
Migrates `FlutterEnging` from manual reference counting to ARC. Migrates
properties from `retain` to strong and `assign` to `weak` (where
referencing an Obj-C object).

No semantic changes, therefore no changes to tests.

Issue: flutter/flutter#137801
cbracken added a commit to cbracken/flutter_engine that referenced this issue Oct 2, 2024
Migrates `FlutterEnging` from manual reference counting to ARC. Migrates
properties from `retain` to strong and `assign` to `weak` (where
referencing an Obj-C object).

No semantic changes, therefore no changes to tests.

Issue: flutter/flutter#137801
cbracken added a commit to cbracken/flutter_engine that referenced this issue Oct 2, 2024
Migrates `FlutterEnging` from manual reference counting to ARC. Migrates
properties from `retain` to strong and `assign` to `weak` (where
referencing an Obj-C object).

No semantic changes, therefore no changes to tests.

Issue: flutter/flutter#137801
cbracken added a commit to cbracken/flutter_engine that referenced this issue Oct 2, 2024
Migrates `FlutterEnging` from manual reference counting to ARC. Migrates
properties from `retain` to strong and `assign` to `weak` (where
referencing an Obj-C object).

No semantic changes, therefore no changes to tests.

Issue: flutter/flutter#137801
cbracken added a commit to cbracken/flutter_engine that referenced this issue Oct 3, 2024
Migrates `FlutterEnging` from manual reference counting to ARC. Migrates
properties from `retain` to strong and `assign` to `weak` (where
referencing an Obj-C object).

No semantic changes, therefore no changes to tests.

Issue: flutter/flutter#137801
auto-submit bot pushed a commit to flutter/engine that referenced this issue Oct 4, 2024
Migrates `FlutterEnging` from manual reference counting to ARC. Migrates properties from `retain` to strong and `assign` to `weak` (where referencing an Obj-C object).

No semantic changes, therefore no changes to tests.

Issue: flutter/flutter#137801

[C++, Objective-C, Java style guides]: https://github.com/flutter/engine/blob/main/CONTRIBUTING.md#style
cbracken added a commit to cbracken/flutter_engine that referenced this issue Oct 5, 2024
Migrates PlatformViewIOS from manual reference counting to ARC.
Eliminates use of scoped_nsobject, scoped_nsprotocol, and WeakNSObject.

Since this is the last non-ARC file in `flutter_framework_source`, this
also eliminates the `flutter_framework_source` target, then also renames
the `flutter_framework_source_arc` target to `flutter_framework_source`
since... it's ALL ARC.

No semantic changes, therefore no changes to tests.

Issue: flutter/flutter#137801
cbracken added a commit to cbracken/flutter_engine that referenced this issue Oct 5, 2024
Migrates PlatformViewIOS from manual reference counting to ARC.
Eliminates use of scoped_nsobject, scoped_nsprotocol, and WeakNSObject.

Since this is the last non-ARC file in `flutter_framework_source`, this
also eliminates the `flutter_framework_source` target, then also renames
the `flutter_framework_source_arc` target to `flutter_framework_source`
since... it's ALL ARC.

No semantic changes, therefore no changes to tests.

Issue: flutter/flutter#137801
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
engine flutter/engine repository. See also e: labels. P2 Important issues not at the top of the work list platform-ios iOS applications specifically team-ios Owned by iOS platform team triaged-ios Triaged by iOS platform team
Projects
None yet
Development

No branches or pull requests

5 participants