flutter_inappwebview
flutter_inappwebview copied to clipboard
InAppWebViewManager: fix nullptr in InAppWebViewManager.dispose
Connection with issue(s)
Resolve issue #2025
Connected to #2025
Testing and Review Notes
Use the demo project to test if this works: https://github.com/Murmurl912/inappwebview_dispose_keepalive_nullptr
Screenshots or Videos
To Do
- [ ] double check the original issue to confirm it is fully satisfied
- [ ] add testing notes and screenshots in PR description to help guide reviewers
- [x] request the "UX" team perform a design review (if/when applicable)
I've tested this locally and it fixes the Android null pointer exception when disposing webview that is using keep alive
Seems like we can expect an update soon, no need to jump package #2268
Fixed in https://github.com/pichillilorenzo/flutter_inappwebview/commit/4ba6e22848aae9c2ca16bc784023b7fa98cf61e3
Thanks
@all-contributors please add @Murmurl912 for code
Before the next release resolves this issue. Anyone using keepAlive should use the following code to safely dispose the InAppWebViewFlutterPlugin. Simply using a try-catch block will prevent the Flutter engine from completely destroy, which can lead to memory leaks and other issues.
It took me several weeks of investigation to discover that a SqliteException(5): while executing statement, database is locked error was actually caused by the Flutter engine not being disposed of properly.
override fun onDestroy() {
try {
super.onDestroy()
} catch (e: Exception) {
// no-op
}
}
Instead, using to following code the remove null webview from inAppWebViewManager :
import com.pichillilorenzo.flutter_inappwebview_android.InAppWebViewFlutterPlugin
override fun onDestroy() {
safeDispose()
super.onDestroy()
}
private fun safeDispose() {
kotlin.runCatching {
val flutterEngine = flutterEngine ?: return
val plugins = flutterEngine.plugins.get(InAppWebViewFlutterPlugin::class.java) as? InAppWebViewFlutterPlugin
?: return
val manager = plugins.inAppWebViewManager ?: return
val clone = manager.keepAliveWebViews.toMap()
for (entry in clone) {
if (entry.value == null) {
manager.keepAliveWebViews.remove(entry.key)
}
}
}
}