-
-
Notifications
You must be signed in to change notification settings - Fork 2.6k
fix: prevent memory leak in SharedDecksActivity by destroying WebView #20108
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
base: main
Are you sure you want to change the base?
Conversation
|
|
||
| Timber.d("Destroying WebView") | ||
|
|
||
| runCatchingWithReport("safeDestroy", onlyIfSilent = true) { |
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 duplicates the following, see if there's a better abstraction which you could use:
Anki-Android/AnkiDroid/src/main/java/com/ichi2/anki/workarounds/SafeWebViewLayout.kt
Lines 169 to 189 in cb64d93
| @MainThread | |
| fun safeDestroy() { | |
| Timber.d("Destroying WebView") | |
| runCatchingWithReport("safeDestroy", onlyIfSilent = true) { | |
| webView.stopLoading() | |
| webView.loadUrl("about:blank") | |
| webView.webChromeClient = null | |
| removeView(webView) | |
| // remove listeners this class exposes | |
| webView.setOnScrollChangeListener(null) | |
| } | |
| // attempt to run destroy() even if the above fails | |
| runCatchingWithReport("safeDestroy", onlyIfSilent = true) { | |
| webView.destroy() | |
| } | |
| } |
|
I've refactored the code to centralize the WebView destruction logic into a static method within 'SafeWebViewLayout.' |
BrayanDSO
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.
I think it's better to use SafeWebViewLayout in SharedDecksActivity, then call safeDestroy() there

Purpose / Description
This PR addresses the memory leak issues in SharedDecksActivity related to WebView usage, as identified in #19965.
Following the experimental fix suggestions, I implemented the
onDestroy()method in SharedDecksActivity and incorporated the cleanup logic based on the pattern found in SafeWebViewLayout.safeDestroy()Fixes
Approach
The focus of this change is to ensure that the WebView is properly disposed of when the Activity is destroyed. I ported the cleanup sequence from SafeWebViewLayout:
about:blank.WebChromeClientandOnScrollChangeListenerto break reference cycles.ViewGroupbefore final destruction.destroy()call inrunCatchingWithReportfor safe execution and logging.How Has This Been Tested?
To verify the fix, I navigated between SharedDecksActivity and DeckPicker 10 times consecutively.
Result:
API 34: Some leaks still persist, but these are attributed to the Context issue as discussed in the issue thread.
However, the specific leaks related to improper WebView teardown have been significantly mitigated.
API 35: leaks are fully resolved with this cleanup in place.
API 34
API 35
Learning (optional, can help others)
Confirmed that applying the SafeWebViewLayout.safeDestroy() pattern directly within an Activity's onDestroy() is an effective experimental approach to mitigate WebView-related memory leaks in AnkiDroid.
Checklist
Please, go through these checks before submitting the PR.