Skip to content

always assign new value to camera property - #315

Open
powerpaul17 wants to merge 1 commit into
capacitor-community:masterfrom
chax-at:always_assign_camera
Open

always assign new value to camera property#315
powerpaul17 wants to merge 1 commit into
capacitor-community:masterfrom
chax-at:always_assign_camera

Conversation

@powerpaul17

Copy link
Copy Markdown

setCamera gets called with null when camera is released. Caused some crashes because it didn't propagate to Preview

@abumalick

Copy link
Copy Markdown

Confirming this diagnosis from a separate investigation — setCamera(null, -1) from CameraActivity.onPause() being swallowed by the if (camera != null) guard leaves Preview.mCamera pointing at a released Camera, and it crashes on the next configuration change:

FATAL EXCEPTION: main
java.lang.RuntimeException: Camera is being used after Camera.release() was called
    at android.hardware.Camera.setDisplayOrientation(Native Method)
    at com.ahm.capacitor.camera.preview.Preview.setCameraDisplayOrientation(Preview.java:145)
    at com.ahm.capacitor.camera.preview.CameraActivity.onConfigurationChanged(CameraActivity.java:423)

One thing worth flagging before this lands: I built 8.0.1 with only this change applied and the same steps still crash, because setCameraDisplayOrientation() dereferences mCamera unconditionally and is reachable from onConfigurationChanged() while the activity is paused. Clearing the field changes the exception type rather than removing the crash:

FATAL EXCEPTION: main
java.lang.NullPointerException: Attempt to invoke virtual method
  'void android.hardware.Camera.setDisplayOrientation(int)' on a null object reference
    at com.ahm.capacitor.camera.preview.Preview.setCameraDisplayOrientation(Preview.java:146)
    at com.ahm.capacitor.camera.preview.CameraActivity.onConfigurationChanged(CameraActivity.java:423)

(Reproduced on a Redmi Note 8 / Android 13: open the preview, background the app, rotate while backgrounded, return, leave the screen.)

So this needs an mCamera == null early return in setCameraDisplayOrientation() alongside it. I've opened #423 which carries that guard plus a separate fix for an IllegalStateException in stop(); it includes an equivalent of this change so it stands alone, but I'd be glad to rebase it onto this PR if you'd rather merge yours first — the diagnosis here was first.

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.

2 participants