Skip to content

fix: Don't capture CameraImpl's scope in the PreviewView hybridRef callback - #4197

Open
rayabelcode wants to merge 1 commit into
margelo:mainfrom
rayabelcode:fix/hybridref-callback-scope
Open

rayabelcode wants to merge 1 commit into
margelo:mainfrom
rayabelcode:fix/hybridref-callback-scope

Conversation

@rayabelcode

@rayabelcode rayabelcode commented Sep 25, 2026 •

Copy link
Copy Markdown
Contributor

Relates to #4180

Problem

CameraImpl creates the hybridRef callback for NativePreviewView inline:

const setHybridRef = useMemo(
  () =>
    callback((r: PreviewView) => {
      previewViewRef.current = r
    }),
  [],
)

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 includes CameraImpl's own scope, which holds the rest props object. So after unmount the callback keeps props reachable, 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):

(GC roots) > (Custom)
  environment in JSFunction          Camera.tsx: callback((r) => { previewViewRef.current = r })
  parentEnvironment in Environment
  10 in Environment                  (CameraImpl scope)
  onError in JSObject(style, device, isActive, orientationSource, zoom, ...)   (CameraImpl props)
  environment in JSFunction          app's onError
  ... bound dispatchSetState > FiberNode (the app screen)

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.

after 48 unmounts setter inside CameraImpl (5.2.3) module-level createHybridRefSetter
unmounted app screens still rooted 48 of 48 0 of 48

In 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-side onPreviewStarted closure, 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 meminfo native 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; CameraPreviewOutput and 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:

function createHybridRefSetter(ref: RefObject<PreviewView | null>) {
  return callback((r: PreviewView) => {
    ref.current = r
  })
}

CameraImpl calls useMemo(() => createHybridRefSetter(previewViewRef), []). The callback native keeps now captures only the ref object, not CameraImpl'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.

…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
@vercel

vercel Bot commented Sep 25, 2026

Copy link
Copy Markdown

@rayabelcode is attempting to deploy a commit to the Margelo Team on Vercel.

A member of the Team first needs to authorize it.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant