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

Conversation

@zhongwuzw
Copy link
Member

Fixes flutter/flutter#54937

There has a retain cycle like below:
SemanticsObject->platformViewSemanticsContainer->platformViewSemanticsContainer.accessibilityElements->SemanticsObject

Another two PRs for memory leaks:
#17954
#18107

@liyuqian liyuqian requested review from cyanglaz and gaaclarke May 6, 2020 17:20

void AccessibilityBridge::clearState() {
for (SemanticsObject* object in objects_.get().allValues) {
object.platformViewSemanticsContainer = nil;
Copy link
Contributor

Choose a reason for hiding this comment

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

We can add this in the dealloc of SemanticsObject

Copy link
Member Author

Choose a reason for hiding this comment

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

Emm, actually we did it in dealloc of SemanticsObject,

[_platformViewSemanticsContainer release];
, but cause of retain cycle, dealloc not executed.

@gaaclarke
Copy link
Member

Please add a test. Previously we haven't been able to easily write tests for this code but now we are able to, see also https://github.com/flutter/engine/blob/master/shell/platform/darwin/ios/framework/Source/SemanticsObjectTest.mm

I'll add a new test file for you in another PR.

Copy link
Member

@gaaclarke gaaclarke left a comment

Choose a reason for hiding this comment

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

Once #18281 is landed you will be able to write a unit test against accessibility bridge. It's hung up in review currently.

@gaaclarke
Copy link
Member

@zhongwuzw Apologies for the delay. I landed that some unit tests for that class so it should be easy to write tests =)

@zhongwuzw
Copy link
Member Author

@gaaclarke 👍 ,I added test, please review.

Copy link
Member

@gaaclarke gaaclarke left a comment

Choose a reason for hiding this comment

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

Nice, thanks! I've looked through this for a few hours and I think I've come up with a better solution.

If you look at this code right here:

        object.platformViewSemanticsContainer = [[[FlutterPlatformViewSemanticsContainer alloc]
            initWithSemanticsObject:object] autorelease];

We can see the obvious cycle.

The better solution is to make FlutterPlatformViewSemanticsContainer have a weak reference to the SemanticsObject, since the SemanticsObject already has a strong reference to him.

The solution originally proposed left the platformViewSemanticsContainer property in a weird state where it was somewhat strong but required outside manipulation like it was weak.

I've given some feedback on this PR, but I'll clean it all up for you so we can get this landed.

- (NSObject<FlutterPlatformView>*)createWithFrame:(CGRect)frame
viewIdentifier:(int64_t)viewId
arguments:(id _Nullable)args {
MockFlutterPlatformView* platformView = [[MockFlutterPlatformView new] autorelease];
Copy link
Member

Choose a reason for hiding this comment

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

Copy link
Member

Choose a reason for hiding this comment

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

done

Copy link
Member Author

Choose a reason for hiding this comment

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

@gaaclarke Emm, I think they are the same thing, they also return autorelease object actually by ARC.

Copy link
Member

Choose a reason for hiding this comment

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

You're right. Sorry, half of our codebase is in ARC half is not. I thought I was looking at non-arc code.

@gaaclarke gaaclarke force-pushed the fix_platform_view_leaks branch 3 times, most recently from 97bf023 to 15a75f4 Compare May 18, 2020 21:35
@gaaclarke
Copy link
Member

@zhongwuzw Ok, the bug is fixed if you want to give it a look. The problem with memory leaks is that sometimes people can start depending on them. Let me double check this still works with the webview plugin.

@gaaclarke gaaclarke force-pushed the fix_platform_view_leaks branch from 15a75f4 to 80db950 Compare May 18, 2020 22:47
@gaaclarke
Copy link
Member

@zhongwuzw Sorry, what I tried seemed correct but broke the plugins. I reverted it. Your unit test and thus your patch is still wrong because it relies on autoreleasing the result of createWithFrame:viewIdentifier:arguments: which isn't how it's suppose to work.

@zhongwuzw
Copy link
Member Author

@zhongwuzw Sorry, what I tried seemed correct but broke the plugins. I reverted it. Your unit test and thus your patch is still wrong because it relies on autoreleasing the result of createWithFrame:viewIdentifier:arguments: which isn't how it's suppose to work.

createWithFrame:viewIdentifier:arguments: actually always return autorelease object, please refer to https://developer.apple.com/library/archive/documentation/Cocoa/Conceptual/MemoryMgmt/Articles/mmRules.html.

@gaaclarke gaaclarke force-pushed the fix_platform_view_leaks branch from 8ec1ed5 to 18197bd Compare May 19, 2020 16:38
@gaaclarke
Copy link
Member

@zhongwuzw thanks for pointing out my mistake with the ARC code. I switched the code back to making FlutterPlatformViewSemanticsContainer.semanticsObject weak. I got stuck yesterday because of the ARC issue.

This way is preferred because it adheres to the contract better. The way it was originally posted the property was strong, but relied in outside manipulation to work. That was fragile because people had to remember to update that property whenever the object_ instance variable is modified. This way that maintenance doesn't have to happen.

I've tested the webview plugin and it works and your test still passes.

@gaaclarke gaaclarke merged commit f93c945 into flutter:master May 20, 2020
@liyuqian liyuqian added the perf: memory Performance issues related to memory label May 20, 2020
@liyuqian liyuqian added the severe: performance Relates to speed or footprint issues. label May 20, 2020
engine-flutter-autoroll added a commit to engine-flutter-autoroll/flutter that referenced this pull request May 21, 2020
engine-flutter-autoroll added a commit to engine-flutter-autoroll/flutter that referenced this pull request May 21, 2020
engine-flutter-autoroll added a commit to engine-flutter-autoroll/flutter that referenced this pull request May 22, 2020
engine-flutter-autoroll added a commit to engine-flutter-autoroll/flutter that referenced this pull request May 22, 2020
engine-flutter-autoroll added a commit to engine-flutter-autoroll/flutter that referenced this pull request May 22, 2020
engine-flutter-autoroll added a commit to engine-flutter-autoroll/flutter that referenced this pull request May 22, 2020
engine-flutter-autoroll added a commit to engine-flutter-autoroll/flutter that referenced this pull request May 22, 2020
engine-flutter-autoroll added a commit to engine-flutter-autoroll/flutter that referenced this pull request May 22, 2020
wandyers pushed a commit to wandyers/engine that referenced this pull request May 23, 2020
engine-flutter-autoroll added a commit to engine-flutter-autoroll/flutter that referenced this pull request May 23, 2020
engine-flutter-autoroll added a commit to engine-flutter-autoroll/flutter that referenced this pull request May 26, 2020
engine-flutter-autoroll added a commit to engine-flutter-autoroll/flutter that referenced this pull request May 26, 2020
engine-flutter-autoroll added a commit to engine-flutter-autoroll/flutter that referenced this pull request May 26, 2020
engine-flutter-autoroll added a commit to engine-flutter-autoroll/flutter that referenced this pull request May 26, 2020
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

cla: yes perf: memory Performance issues related to memory severe: performance Relates to speed or footprint issues.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Potential memory leak issue in iOS

5 participants