Skip to content

FlutterMethodChannel may cause memory leak in iOS #26007

Closed

Description

@lext-7

See FlutterChannels.mm

FlutterBinaryMessenger is strongly referenced in FlutterMethodChannel. If the FlutterBinaryMessenger is a FlutterViewController, then this channel should be strongly referenced so that it can work, as the result the FlutterViewController is strongly referenced too. There is no callback for the controller finally releasing so that I can remove this channel.

Is that FlutterBinaryMessenger in FlutterMethodChannel supposed to be weak reference.

Activity

  1. added
    frameworkflutter/packages/flutter repository. See also f: labels.
    c: performanceRelates to speed or footprint issues (see "perf:" labels)
    on Jan 3, 2019
  2. added this to the milestone on Jan 3, 2019
  3. zoechi commented on Jan 3, 2019

    @zoechi
    Contributor

    Please add the output of flutter doctor -v.

  4. wouterhardeman commented on Jan 3, 2019

    @wouterhardeman

    I think this will be fixed by: flutter-team-archive/engine#6879

  5. lext-7 commented on Jan 3, 2019

    @lext-7
    ContributorAuthor

    @wouterhardeman There is no change in the file I mention in this PR.

  6. added
    engineflutter/engine related. See also e: labels.
    on Jan 4, 2019
  7. dnfield commented on Jan 11, 2019

    @dnfield
    Contributor

    Yes, we need to find a good way to make this a weak reference. The way we typically handle it in the embedding (fml::WeakPtr) can't be used because it's C++/Objective C++ that we can't leak out into the public API.

  8. added
    platform-iosiOS applications specifically
    e: embedderUsers of the Embedder API
    a: existing-appsIntegration with existing apps via the add-to-app flow
    and removed
    e: embedderUsers of the Embedder API
    platform-iosiOS applications specifically
    on Jan 11, 2019
  9. 18 remaining items

  10. dnfield commented on Jun 13, 2019

    @dnfield
    Contributor

    I reopened this because I thought I had a local reproduction. Sitting with @gaaclarke, I'm not actually able to reproduce it anymore.

    Can anyone who has been invovled in this provide a simple reproduction for us to work on? If not, I think we can assume this has either been fixed, or it won't result in correct usage of the API.

  11. added
    waiting for responseThe Flutter team cannot make further progress on this issue until the original reporter responds
    on Jun 13, 2019
  12. dnfield commented on Jun 13, 2019

    @dnfield
    Contributor

    (What would be ideal is a test case in dev/integration_tests/ios_add2app that fails without this being fixed).

  13. gaaclarke commented on Jun 14, 2019

    @gaaclarke
    Member

    I may have figured out what this bug is referring to. I've written up a UINavigationController that will push a FlutterViewController onto its stack. When the FlutterViewController is popped it is not deleted immediately. The FlutterEngine and the channel both have retains on the FlutterViewController.

    I was able to force the FlutterViewController to dealloc by clearing out the channel and the engine's view controller: https://gist.github.com/gaaclarke/dd650342fba18ea640c7b3923995cc7d

    I'm not sure what we want to do about this yet, but there is the example to get the FlutterViewController to dealloc in the meantime.

  14. xster commented on Jun 14, 2019

    @xster
    Member

    Ideally the inner FlutterViewController should free itself as soon as it pops (rather than its parent, which owns the FlutterEngine) popping. Someone has to call [flutterEngine setViewController:nil] of course but that seems reasonable.

    Is this not an issue anymore if our primary doc had people pass the flutterEngine into https://gist.github.com/gaaclarke/dd650342fba18ea640c7b3923995cc7d#file-gistfile1-txt-L35?

  15. removed
    waiting for responseThe Flutter team cannot make further progress on this issue until the original reporter responds
    on Jun 14, 2019
  16. gaaclarke commented on Jun 14, 2019

    @gaaclarke
    Member

    @xster Yes, if we use the FlutterEngine as the binary messenger we don't have to do any of the fiddling with the channel, just calling [flutterEngine setViewController:nil] is sufficient to get the FlutterViewController to dealloc.

  17. gaaclarke commented on Jun 19, 2019

    @gaaclarke
    Member

    After flutter-team-archive/engine#9347 channels still leak, but they won't be holding onto FlutterViewControllers anymore. So, even passing in the FlutterViewController as the binary messenger, you'll be able to dealloc the FlutterViewController (assuming your Engine still isn't referencing it).

  18. gaaclarke commented on Jun 28, 2019

    @gaaclarke
    Member
  19. modified the milestones: , December 2019 (Add-to-App) on Aug 29, 2019
  20. github-actions commented on Aug 28, 2021

    @github-actions

    This thread has been automatically locked since there has not been any recent activity after it was closed. If you are still experiencing a similar issue, please open a new bug, including the output of flutter doctor -v and a minimal reproduction of the issue.

  21. locked as resolved and limited conversation to collaborators on Aug 28, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    a: existing-appsIntegration with existing apps via the add-to-app flowc: performanceRelates to speed or footprint issues (see "perf:" labels)e: embedderUsers of the Embedder APIengineflutter/engine related. See also e: labels.platform-iosiOS applications specifically

    Type

    No type

    Projects

    No projects

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions