Skip to content
This repository was archived by the owner on Feb 25, 2025. It is now read-only.

Commit 80dd84e

Browse files
Unregister onFrameAvailable callbacks when a TextureEntry is released.
Otherwise the callbacks may be called after FlutterNativeView is destroyed and is null. Also defensively check for whether the texture is already released in the callback because the callback may be called from another thread by a stale reference (see the comment). This closes flutter/flutter#20951.
1 parent 774a704 commit 80dd84e

File tree

1 file changed

+11
-0
lines changed

1 file changed

+11
-0
lines changed

shell/platform/android/io/flutter/view/FlutterView.java

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -997,6 +997,14 @@ final class SurfaceTextureRegistryEntry implements TextureRegistry.SurfaceTextur
997997
this.surfaceTexture.setOnFrameAvailableListener(new SurfaceTexture.OnFrameAvailableListener() {
998998
@Override
999999
public void onFrameAvailable(SurfaceTexture texture) {
1000+
// NB: This callback may be called on an arbitrary thread.
1001+
if (SurfaceTextureRegistryEntry.this.surfaceTexture.isReleased()) { // isReleased is synchronized in JNI code.
1002+
// Even though we make sure to unregister the callback before releasing,
1003+
// SurfaceTexture has a data race when accessing the callback, so the callback may
1004+
// still be called by a stale reference after surfaceTexture.isRelease() and mNativeView==null.
1005+
// See b/113099095.
1006+
return;
1007+
}
10001008
nativeMarkTextureFrameAvailable(mNativeView.get(), SurfaceTextureRegistryEntry.this.id);
10011009
}
10021010
});
@@ -1019,6 +1027,9 @@ public void release() {
10191027
}
10201028
released = true;
10211029
nativeUnregisterTexture(mNativeView.get(), id);
1030+
// Otherwise onFrameAvailableListener might be called after mNativeView==null
1031+
// (https://github.com/flutter/flutter/issues/20951). See also the check in onFrameAvailable.
1032+
surfaceTexture.setOnFrameAvailableListener(null);
10221033
surfaceTexture.release();
10231034
}
10241035
}

0 commit comments

Comments
 (0)