8000 [ios]Share extension official example leaks memory causing a crash · Issue #165829 · flutter/flutter · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

[ios]Share extension official example leaks memory causing a crash #165829

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

Closed
hellohuanlin opened this issue Mar 24, 2025 · 4 comments
Closed

[ios]Share extension official example leaks memory causing a crash #165829

hellohuanlin opened this issue Mar 24, 2025 · 4 comments
Assignees
Labels
a: release Challenges faced when attempting to productionize an app c: crash Stack traces logged to the console d: api docs Issues with https://api.flutter.dev/ d: examples Sample code and demos P1 High-priority issues at the top of the work list perf: memory Performance issues related to memory platform-ios iOS applications specifically team-ios Owned by iOS platform team triaged-ios Triaged by iOS platform team

Comments

@hellohuanlin
Copy link
Contributor
hellohuanlin commented Mar 24, 2025

Steps to reproduce

Follow https://docs.flutter.dev/platform-integration/ios/app-extensions to implement share extension. Use Release mode so it uses less memory to be run on device (220M limit). Launch it a few times and it will crash.

Expected results

Should not crash.

Actual results

It crashes.
From the memory graph, the VC is not released after being dismissed:

Image

@hellohuanlin hellohuanlin added the platform-ios iOS applications specifically label Mar 24, 2025
@hellohuanlin hellohuanlin changed the title [ios]Share extension official example memory leak [ios]Share extension official example leaks memory Mar 24, 2025
@hellohuanlin hellohuanlin changed the title [ios]Share extension official example leaks memory [ios]Share extension official example leaks memory causing a crash Mar 24, 2025
@hellohuanlin
Copy link
Contributor Author

Duplicate of #165086

@hellohuanlin hellohuanlin marked this as a duplicate of #165086 Mar 25, 2025
@hellohuanlin
Copy link
Contributor Author

Not duplicate - the other issue doesn't require launching the share extension, so isn't related to ShareVC being leaked.

@hellohuanlin hellohuanlin reopened this Mar 25, 2025
@darshankawar darshankawar added d: api docs Issues with https://api.flutter.dev/ d: examples Sample code and demos team-ios Owned by iOS platform team c: crash Stack traces logged to the console perf: memory Performance issues related to memory a: release Challenges faced when attempting to productionize an app labels Mar 26, 2025
@jmagman jmagman added the P1 High-priority issues at the top of the work list label Mar 31, 2025
@hellohuanlin
Copy link
Contributor Author
hellohuanlin commented Apr 8, 2025

It turns out that this is because share extensions are allowed to keep running in background after being dismissed (in case of background processing).

To deallocate the VC, we have to either completeRequest or cancelRequest. There isn't a good designated function where you call these 2 APIs, if you are extending from UIViewController (rather than the default SLComposeServiceViewController).

However, I did find viewDidDisappear a good place to do so - surprisingly viewDidDisappear is only called if we dismiss by gesture, but not called if we dismiss it programmatically, which is an undocumented behavior. This means we can simply call cancelRequest in viewDidDisappear, without worrying about it being called twice:

  override func viewDidDisappear(_ animated: Bool) {
    super.viewDidDisappear(animated)
    self.extensionContext?.cancelRequest(withError: NSError(domain: Bundle.main.bundleIdentifier!, code: 0))
  }

Note that we have to pass in the bundle ID as the error domain, otherwise it's gonna crash. Alternatively, we can dismiss using completeRequest API. Though cancelRequest is semantically more accurate.

I will update the instruction on the website.

sfshaza2 pushed a commit to flutter/website that referenced this issue Apr 15, 2025
…rolling behavior (#11882)

This PR does 2 things: 

1. Fix a memory leak in the instruction. 
2. Add instruction on advanced scrolling behavior. I simply link the
instruction (instead of copying it since I don't think it's a very
common use case).

_Issues fixed by this PR (if any):_
flutter/flutter#165829

_PRs or commits this PR depends on (if any):_

## Presubmit checklist

- [ ] This PR is marked as draft with an explanation if not meant to
land until a future stable release.
- [x] This PR doesn’t contain automatically generated corrections
(Grammarly or similar).
- [x] This PR follows the [Google Developer Documentation Style
Guidelines](https://developers.google.com/style) — for example, it
doesn’t use _i.e._ or _e.g._, and it avoids _I_ and _we_ (first person).
- [x] This PR uses [semantic line
breaks](https://github.com/dart-lang/site-shared/blob/main/doc/writing-for-dart-and-flutter-websites.md#semantic-line-breaks)
of 80 characters or fewer.
@vashworth vashworth added the triaged-ios Triaged by iOS platform team label Apr 25, 2025
@vashworth
Copy link
Contributor

Website documentation has been updated.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
a: release Challenges faced when attempting to productionize an app c: crash Stack traces logged to the console d: api docs Issues with https://api.flutter.dev/ d: examples Sample code and demos P1 High-priority issues at the top of the work list perf: memory Performance issues related to memory platform-ios iOS applications specifically team-ios Owned by iOS platform team triaged-ios Triaged by iOS platform team
Projects
None yet
Development

No branches or pull requests

4 participants
0