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

Commit b75e8ff

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 b75e8ff

1 file changed

Lines changed: 11 additions & 1 deletion

File tree

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

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -997,9 +997,16 @@ final class SurfaceTextureRegistryEntry implements TextureRegistry.SurfaceTextur
997997
this.surfaceTexture.setOnFrameAvailableListener(new SurfaceTexture.OnFrameAvailableListener() {
998998
@Override
999999
public void onFrameAvailable(SurfaceTexture texture) {
1000+
if (released) {
1001+
// Even though we make sure to unregister the callback before releasing,
1002+
// SurfaceTexture has a data race when accessing the callback, so the callback may
1003+
// still be called by a stale reference after released==true and mNativeView==null.
1004+
// See b/113099095.
1005+
return;
1006+
}
10001007
nativeMarkTextureFrameAvailable(mNativeView.get(), SurfaceTextureRegistryEntry.this.id);
10011008
}
1002-
});
1009+
}, new Handler());
10031010
}
10041011

10051012
@Override
@@ -1019,6 +1026,9 @@ public void release() {
10191026
}
10201027
released = true;
10211028
nativeUnregisterTexture(mNativeView.get(), id);
1029+
// Otherwise onFrameAvailableListener might be called after mNativeView==null
1030+
// (https://github.com/flutter/flutter/issues/20951). See also the check in onFrameAvailable.
1031+
surfaceTexture.setOnFrameAvailableListener(null);
10221032
surfaceTexture.release();
10231033
}
10241034
}

0 commit comments

Comments
 (0)