Skip to content

Conversation

wolfie82
Copy link

@wolfie82 wolfie82 commented Jun 23, 2023

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

  • 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 relevant style guides and ran the auto-formatter. (Unlike the flutter/flutter repo, the flutter/packages repo does use dart format.)
  • I signed the CLA.
  • The title of the PR starts with the name of the package surrounded by square brackets, e.g. [shared_preferences]
  • I listed at least one issue that this PR fixes in the description above.
  • I updated pubspec.yaml with an appropriate new version according to the pub versioning philosophy, or this PR is exempt from version changes.
  • I updated CHANGELOG.md to add a description of the change, following repository CHANGELOG style.
  • I updated/added relevant documentation (doc comments with ///).
  • I added new tests to check the change I am making, or this PR is test-exempt.
  • All existing and new tests are passing.

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

@wolfie82 wolfie82 changed the title fix: Clean up subscriptions when disposing, instead of firing and for… [google_maps_flutter] Fix for memory leak impacting all platforms due to subscriptions not getting cleaned up Jun 23, 2023
@wolfie82 wolfie82 marked this pull request as ready for review June 23, 2023 04:47
@wolfie82 wolfie82 requested a review from stuartmorgan-g as a code owner June 23, 2023 04:47
@wolfie82
Copy link
Author

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).

@stuartmorgan-g
Copy link
Collaborator

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.

@wolfie82
Copy link
Author

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.

@wolfie82
Copy link
Author

Hi @stuartmorgan. I added some unit tests and it looks like all the build checks have passed except for submit-queue, which I think is outside my control. Thanks again!

Copy link
Collaborator

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.
Copy link
Collaborator

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`.
Copy link
Collaborator

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) {
Copy link
Collaborator

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?

Copy link
Author

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.

@stuartmorgan-g
Copy link
Collaborator

@wolfie82 Are you still planning on updating this to address the latest feedback?

@MattyBoy4444
Copy link

Any update on this?

@stuartmorgan-g
Copy link
Collaborator

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!

@stuartmorgan-g stuartmorgan-g marked this pull request as draft August 17, 2023 18:48
@Hixie
Copy link
Contributor

Hixie commented Nov 7, 2023

@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!

@Hixie Hixie closed this Nov 7, 2023
auto-submit bot pushed a commit that referenced this pull request May 8, 2025
… 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
androidseb pushed a commit to androidseb/packages that referenced this pull request Jun 8, 2025
… 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
FMorschel pushed a commit to FMorschel/packages that referenced this pull request Jun 9, 2025
… 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
Ortes pushed a commit to Ortes/packages that referenced this pull request Jun 25, 2025
… 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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants