-
Notifications
You must be signed in to change notification settings - Fork 6k
Fix iOS platform view not deallocated #18164
Conversation
|
|
||
| void AccessibilityBridge::clearState() { | ||
| for (SemanticsObject* object in objects_.get().allValues) { | ||
| object.platformViewSemanticsContainer = nil; |
There was a problem hiding this comment.
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
There was a problem hiding this comment.
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]; |
|
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. |
gaaclarke
left a comment
There was a problem hiding this 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.
|
@zhongwuzw Apologies for the delay. I landed that some unit tests for that class so it should be easy to write tests =) |
|
@gaaclarke 👍 ,I added test, please review. |
gaaclarke
left a comment
There was a problem hiding this 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.
shell/platform/darwin/ios/framework/Source/accessibility_bridge_test.mm
Outdated
Show resolved
Hide resolved
shell/platform/darwin/ios/framework/Source/accessibility_bridge_test.mm
Outdated
Show resolved
Hide resolved
shell/platform/darwin/ios/framework/Source/accessibility_bridge_test.mm
Outdated
Show resolved
Hide resolved
shell/platform/darwin/ios/framework/Source/accessibility_bridge_test.mm
Outdated
Show resolved
Hide resolved
shell/platform/darwin/ios/framework/Source/accessibility_bridge_test.mm
Outdated
Show resolved
Hide resolved
| - (NSObject<FlutterPlatformView>*)createWithFrame:(CGRect)frame | ||
| viewIdentifier:(int64_t)viewId | ||
| arguments:(id _Nullable)args { | ||
| MockFlutterPlatformView* platformView = [[MockFlutterPlatformView new] autorelease]; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Your example is using autorelease (which generally is proper) but other plugins are not doing this: https://github.com/flutter/plugins/blob/531ff2bc80afb3181218a1ff46e195303348ae0c/packages/google_maps_flutter/google_maps_flutter/ios/Classes/GoogleMapController.m#L37
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
done
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.
97bf023 to
15a75f4
Compare
|
@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. |
15a75f4 to
80db950
Compare
|
@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 |
|
8ec1ed5 to
18197bd
Compare
|
@zhongwuzw thanks for pointing out my mistake with the ARC code. I switched the code back to making 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 I've tested the webview plugin and it works and your test still passes. |
Co-authored-by: Aaron Clarke <[email protected]>
Fixes flutter/flutter#54937
There has a retain cycle like below:
SemanticsObject->platformViewSemanticsContainer->platformViewSemanticsContainer.accessibilityElements->SemanticsObject
Another two PRs for memory leaks:
#17954
#18107