flutter_boost
flutter_boost copied to clipboard
[Android]Flutter页面销毁时,无法dispose PlatformView
Steps to Reproduce
A small application to reproduce the bug(最小化可复现的demo)
将exmaple中flutterPage对应的页面改成NativeViewExample后,点击open flutter page按钮打开NativeView页面,再退出,重复打开退出操作。会发现页面越来越卡,内存占用越来越大。
Flutter Boost Version 4.2.2
Target Platform: Android
Reason
页面退出的时候,会在onDestroy中触发perfromDetach方法,其中会调用PlatformViewsController的detach方法
public void detach() {
if (platformViewsChannel != null) {
platformViewsChannel.setPlatformViewsHandler(null);
}
destroyOverlaySurfaces();
platformViewsChannel = null;
context = null;
textureRegistry = null;
}
将PlatformViewsHandler设置为null。这个就导致PlatformViewsChannel中再接收到Flutter层传来的dispose消息时,因为handler为null,直接return了,所以无法dispose那些PlatformView。
private final MethodChannel.MethodCallHandler parsingHandler =
new MethodChannel.MethodCallHandler() {
@Override
public void onMethodCall(@NonNull MethodCall call, @NonNull MethodChannel.Result result) {
// If there is no handler to respond to this message then we don't need to
// parse it. Return.
if (handler == null) {
return;
}
......
}
};
Fix
在FlutterBoostActivity和FlutterBoostFragment的onDestroy方法中,使用反射,调用PlatformViewController.flushAllViews方法。
修复后,虽然能够dispose了并且没有了Java层的内存泄漏,但发现Native Memory仍会上涨,感觉是Surface相关的内存没有释放,不知道是哪的原因。
iOS不知道有没有相同问题
#1883
#1883 做了个实验,不使用 flutter_boost,直接Native 打开 Platformview 然后返回 native ,也不会 dealloc,但是 Native 打开 Flutter 页面,然后 Flutter 打开 Platformview,然后关闭 Platformview 会 dealloc。
还是有问题