alibaba / alibaba/flutter_boost

[Android]Flutter页面销毁时,无法dispose PlatformView

Open
#1,834 4 comments 0 reactions 0 assignees View on GitHub
Dominant language
Dart
Stars
7.2k
Forks
1.3k
PR merge metrics
No merged PRs in 30d

Description

## 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方法
``` java
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。
``` java
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相关的内存没有释放,不知道是哪的原因。

Contributor guide

No contributing guide indexed for this repository

Research direction

Trace the Android lifecycle from FlutterBoostActivity and FlutterBoostFragment onDestroy through PlatformViewsController.detach and PlatformViewsChannel handling. Review the reported flushAllViews workaround and reproduce repeated NativeViewExample open/close cycles. Done means PlatformViews are disposed and both Java and native memory, including Surface-related memory, no longer grow.

Written by the indexing model from the issue text.

Assessment

Tech stack
android, dart, flutter, java
Domain
mobile
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.