-
-
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
Conversation
|
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
AnkiDroid/src/main/java/com/ichi2/anki/workarounds/SafeWebViewLayout.kt
Outdated
Show resolved
Hide resolved
AnkiDroid/src/main/java/com/ichi2/anki/workarounds/SafeWebViewLayout.kt
Outdated
Show resolved
Hide resolved
AnkiDroid/src/main/java/com/ichi2/anki/workarounds/SafeWebViewLayout.kt
Outdated
Show resolved
Hide resolved
|
Thanks for the feedback, David! I've updated the code to address your suggestions: |
david-allison
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'm happy, thanks so much! One minor request
AnkiDroid/src/main/java/com/ichi2/anki/workarounds/SafeWebViewLayout.kt
Outdated
Show resolved
Hide resolved
b2f44d0 to
68ad929
Compare
|
I have squash merged and force pushed this to make it easier to merge. If there are review comments, I'm happy to take responsibility for them, since I force pushed Cheers! |

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.