-
Notifications
You must be signed in to change notification settings - Fork 3.5k
[google_maps_flutter] Fix for memory leak impacting all platforms due to subscriptions not getting cleaned up #4281
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
Conversation
I'm having a bit of trouble writing a test for this given the public API surface of the internals that were fixed. @stuartmorgan I hate to open a PR without meeting all the criteria, however, I'm happy to move this forward with a bit of guidance. I think this PR is extremely important as it fixes a severe memory leak. In our app, if we navigate away from the GoogleMap and then back again * 3 on an iPhone SE, it will crash with an out of memory exception due to these subscriptions not getting closed and therefore retaining state from the calling widget (see flutter/flutter#129089). |
Since the goal is to ensure that the subscriptions are cancelled, it looks like you could test it by vending mock/fake subscriptions in a unit test, and validating that they all have cancel called on them. |
Appreciate that. This one is a case, at least for me, where the test is harder than the fix. I'll give it a fresh pair of eyes on Monday. I was aiming for a test that would leverage the new controller update, but the API surface that's public to the test is what makes this complicated. I could use the fake platform test implementation already existing, just figuring out how to wire everything up. |
Hi @stuartmorgan. I added some unit tests and it looks like all the build checks have passed except for |
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.
This file should be called controller_test.dart
, since we generally want unit test files to correspond directly to the files they test.
@@ -18,6 +18,35 @@ class GoogleMapController { | |||
/// The mapId for this controller | |||
final int mapId; | |||
|
|||
/// State map of the stream subscriptions for this controller, used internally. |
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.
Internal implementation details should not be public properties; this would make it impossible for us to ever change without a major version bump.
/// | ||
/// The map stores a [StreamSubscription] as key and a [bool] as value. | ||
/// Each [StreamSubscription] represents a subscription to an event from the native side. | ||
/// The corresponding [bool] indicates whether the subscription has been canceled `true` or not `false`. |
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.
It's unclear to me why the boolean exists, since once it's been canceled there is no reason to track it any more; just being in a set is already a boolean state.
Is this only for the unit test? If so, the unit test should be testing that the subscription is actually canceled, via fake subscription that keep track of cancel
calls, rather than testing an unrelated property that we just hope corresponds to that. As currently written, it would be easy to break the actual functionality without breaking the tests.
@@ -398,6 +398,9 @@ class _GoogleMapState extends State<GoogleMap> { | |||
} | |||
|
|||
Future<void> onPlatformViewCreated(int id) async { | |||
if (!mounted) { |
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.
How is this related to the PR?
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.
See flutter/flutter#115283 - I can add a test for this.
@wolfie82 Are you still planning on updating this to address the latest feedback? |
Any update on this? |
Marking as a draft for now just to get this off of our review queue. Please mark it as ready for review once you've had a chance to update it! |
@wolfie82 Thanks for your contribution. I'm going to close this PR for now, but I'll leave a pointer on the issues you listed in case someone wants to take it over. Thanks again! |
… to subscriptions not getting cleaned up (#8972) This PR is #4281 follow-up,Fix map memory leak *List which issues are fixed by this PR. You must list at least one issue.* [#129089](flutter/flutter#129089) Fixes [#115283](flutter/flutter#115283) [#92788](flutter/flutter#92788) ## Pre-Review Checklist
… to subscriptions not getting cleaned up (flutter#8972) This PR is flutter#4281 follow-up,Fix map memory leak *List which issues are fixed by this PR. You must list at least one issue.* [#129089](flutter/flutter#129089) Fixes [#115283](flutter/flutter#115283) [#92788](flutter/flutter#92788) ## Pre-Review Checklist
… to subscriptions not getting cleaned up (flutter#8972) This PR is flutter#4281 follow-up,Fix map memory leak *List which issues are fixed by this PR. You must list at least one issue.* [#129089](flutter/flutter#129089) Fixes [#115283](flutter/flutter#115283) [#92788](flutter/flutter#92788) ## Pre-Review Checklist
… to subscriptions not getting cleaned up (flutter#8972) This PR is flutter#4281 follow-up,Fix map memory leak *List which issues are fixed by this PR. You must list at least one issue.* [#129089](flutter/flutter#129089) Fixes [#115283](flutter/flutter#115283) [#92788](flutter/flutter#92788) ## Pre-Review Checklist
Prior to this change, GoogleMaps would create a condition where its state wasn't cleaned up. Any widget that was leveraging GoogleMap wasn't cleaned up if it relied on a subscription, such as onCameraIdle, onCameraMoved, etc. This was creating crashes for our team specifically.
List which issues are fixed by this PR. You must list at least one issue.
#129089
#115283
#92788
If you had to change anything in the flutter/tests repo, include a link to the migration guide as per the breaking change policy.
Pre-launch Checklist
dart format
.)[shared_preferences]
pubspec.yaml
with an appropriate new version according to the pub versioning philosophy, or this PR is exempt from version changes.CHANGELOG.md
to add a description of the change, following repository CHANGELOG style.///
).If you need help, consider asking for advice on the #hackers-new channel on Discord.