fix: Don't capture CameraImpl's scope in the PreviewView hybridRef callback - #4197
Open
rayabelcode wants to merge 1 commit into
Open
rayabelcode wants to merge 1 commit into
rayabelcode wants to merge 1 commit into
Conversation
…llback The hybridRef callback passed to NativePreviewView was created inside CameraImpl. Native keeps that callback for the lifetime of the native view, and on Hermes its environment chain includes CameraImpl's scope, so the component's props, and every app callback passed to <Camera>, stayed reachable after unmount. Create the callback in a module-level factory that captures only the ref object. Behavior is unchanged. Relates to margelo#4180
|
@rayabelcode is attempting to deploy a commit to the Margelo Team on Vercel. A member of the Team first needs to authorize it. |
8 of 18 tasks
This branch has not been deployed
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Relates to #4180
Problem
CameraImplcreates thehybridRefcallback forNativePreviewViewinline:Native keeps this callback for as long as the native preview view lives, and that view is not released when
<Camera>unmounts. On Hermes the arrow function's environment chain includesCameraImpl's own scope, which holds the restpropsobject. So after unmount the callback keepspropsreachable, and with it every callback the app passed to<Camera>(onError,onInterruptionStarted, ...) and whatever those close over. In our app that kept the whole screen that rendered<Camera>alive after every unmount.Retainer chain from a Hermes heap snapshot (React DevTools registry excluded):
Evidence
Android, react-native-vision-camera 5.2.3, Hermes, New Architecture, Pixel 10 Pro XL emulator (API 37). Same method for both columns: debug build, 24 scans (48
<Camera>mounts and unmounts) with nothing attached, then DevTools attached once and a single heap snapshot. Counts are objects reachable from native(Custom)roots with the React DevTools backend map excluded.CameraImpl(5.2.3)createHybridRefSetterIn the left column the app already passed a module-level function as
onPreviewStarted, so this setter was the only remaining per-mount native root that reached the app screen (chain above). The right-hand run also carried the Kotlin changes from #4185 and #4186 and a JS cleanup that clears the preview view's props on unmount. Those alone do not free the screen: an earlier run had them with the inline setter and an app-sideonPreviewStartedclosure, and every screen was still rooted. I have not run the module-level setter in isolation; the claim is that it removes the last root, not that it is sufficient on its own for every app.Release-build native memory did not measurably change:
dumpsys meminfonative heap growth per scan stayed in the same range with and without this change (one run each). This PR fixes JS-side retention of app code, not the native growth tracked in #4180;CameraPreviewOutputand the gesture-controller array are still held per mount through the view's props, which is native-side.Change
Create the setter in a module-level function that takes only the ref:
CameraImplcallsuseMemo(() => createHybridRefSetter(previewViewRef), []). The callback native keeps now captures only the ref object, notCameraImpl's scope. Behavior is unchanged: the same ref is set to the same value. Only measured on Android; the setter is shared JS, so iOS should behave the same, but that is untested.