react-native-vision-camera icon indicating copy to clipboard operation
react-native-vision-camera copied to clipboard

🐛 Vision Camera crashes when built with Xcode 14

Open jpudysz opened this issue 1 year ago • 13 comments

What were you trying to do?

I was trying to build my app with Xcode 14.

Reproduceable Code

No super fancy code. Simply render the camera with a frame processor. I'm using QR code scanner:


const [frameProcessor, barcodes] = useScanBarcodes([BarcodeFormat.CODE_128])

return (
    <Camera
       device={device}
       isActive={isActive}
       frameProcessorFps={5}
       frameProcessor={frameProcessor}
       style={StyleSheet.absoluteFillObject}
    />
)

What happened instead?

The app crashed with an error from HostFunction.

I can confirm that code works as expected when built with Xcode 13.

Relevant log output

2022-09-15 10:13:26.797643+0200 x[6471:486752] [native] VisionCamera.didSetProps(_:): Updating 13 prop(s)...
2022-09-15 10:13:26.797882+0200 x[6471:487061] [native] VisionCamera.configureCaptureSession(): Configuring Session...
2022-09-15 10:13:26.797894+0200 x[6471:487061] [native] VisionCamera.configureCaptureSession(): Initializing Camera with device com.apple.avfoundation.avcapturedevice.built-in_video:0...
2022-09-15 10:13:26.797906+0200 x[6471:487061] [native] VisionCamera.configureCaptureSession(): Adding Video input...
2022-09-15 10:13:26.798369+0200 x[6471:487075] FrameProcessorBindings: Setting new frame processor...
2022-09-15 10:13:26.798393+0200 x[6471:487075] FrameProcessorBindings: Adapting Shareable value from function (conversion to worklet)...
2022-09-15 10:13:26.798517+0200 x[6471:487075] FrameProcessorBindings: Successfully created worklet!
2022-09-15 10:13:26.798554+0200 x[6471:487097] FrameProcessorBindings: Converting worklet to Objective-C callback...
2022-09-15 10:13:26.799270+0200 x[6471:487097] FrameProcessorBindings: Frame processor set!
2022-09-15 10:13:26.838111+0200 x[6471:487061] [native] VisionCamera.configureCaptureSession(): Adding Video Data output...
2022-09-15 10:13:26.838707+0200 x[6471:487061] [native] VisionCamera.invokeOnInitialized(): Camera initialized!
2022-09-15 10:13:26.838926+0200 x[6471:487061] [native] VisionCamera.configureCaptureSession(): Session successfully configured!
2022-09-15 10:13:26.839396+0200 x[6471:487061] [native] VisionCamera.configureFormat(): Configuring Format...
2022-09-15 10:13:26.839401+0200 x[6471:487061] [native] VisionCamera.configureDevice(): Configuring Device...
2022-09-15 10:13:26.839427+0200 x[6471:487061] [native] VisionCamera.configureDevice(): Device successfully configured!
2022-09-15 10:13:26.839689+0200 x[6471:487061] [native] VisionCamera.didSetProps(_:): Starting Session...
2022-09-15 10:13:27.007614+0200 x[6471:487061] [native] VisionCamera.didSetProps(_:): Started Session!
libc++abi: terminating with uncaught exception of type facebook::jsi::JSError: Exception in HostFunction: Javascript worklet error

Error: Javascript worklet error
    at _f (native)

Error: Exception in HostFunction: Javascript worklet error

Error: Javascript worklet error
    at _f (native)
    at _f (native)
terminating with uncaught exception of type facebook::jsi::JSError: Exception in HostFunction: Javascript worklet error

Error: Javascript worklet error
    at _f (native)

Error: Exception in HostFunction: Javascript worklet error

Error: Javascript worklet error
    at _f (native)
    at _f (native)
(Recorded stack frame) 


I've debugged the code and it crashes in FrameProcessorUtils.mm:

cb.callWithThis(runtime, cb, jsi::Object::createFromHostObject(runtime, frameHostObject));

Null pointer exception



### Device

iPhone 13

### VisionCamera Version

2.14.1

### Additional information

- [ ] I am using Expo
- [X] I have read the [Troubleshooting Guide](https://mrousavy.github.io/react-native-vision-camera/docs/guides/troubleshooting)
- [X] I agree to follow this project's [Code of Conduct](https://github.com/mrousavy/react-native-vision-camera/blob/main/CODE_OF_CONDUCT.md)
- [X] I searched for [similar issues in this repository](https://github.com/mrousavy/react-native-vision-camera/issues) and found none.

jpudysz avatar Sep 15 '22 09:09 jpudysz

Hello @jpudysz, could you add your react-native/react-native-vision-camera versions please ? :)

baylesa-dev avatar Sep 15 '22 09:09 baylesa-dev

Sure!

"react-native": "0.69.5",
"react-native-vision-camera": "2.14.1",
"vision-camera-code-scanner": "0.2.0"

jpudysz avatar Sep 15 '22 09:09 jpudysz

Similar issue here.

[javascript] Frame Processor threw an error: Can't find variable: __xxxx

icc-guerrero avatar Sep 15 '22 10:09 icc-guerrero

I'm able to reproduce the issue (repro https://github.com/baylesa-dev/rn_vision_camera_xcode_14)

Edit: Just tried with https://github.com/mrousavy/vision-camera-image-labeler, and it works perfectly: the issue seems to be related to the swift implementation of https://github.com/rodgomesc/vision-camera-code-scanner

baylesa-dev avatar Sep 15 '22 11:09 baylesa-dev

Nice catch @baylesa-dev! The problem is that we can't even call this plugin. It breaks before callback: https://github.com/rodgomesc/vision-camera-code-scanner/blob/0571128d46a4a7564aca97d9271c8f7c0b67a5d7/ios/VisionCameraCodeScanner.swift#L11

jpudysz avatar Sep 15 '22 13:09 jpudysz

Yep!

console.log(typeof __labelImage) // => function
console.log(typeof __scanCodes) // => undefined

baylesa-dev avatar Sep 15 '22 13:09 baylesa-dev

I also checked vision-camera-face-detector, and it crashed immediately. The only difference between all these plugins is that vision-camera-image-labeler has been written with Objective-C instead of Swift.

I suspect that VISION_EXPORT_SWIFT_FRAME_PROCESSOR is not registering frame processors correctly or something else breaks this registration in Xcode 14.

jpudysz avatar Sep 15 '22 15:09 jpudysz

npx vision-camera-plugin-builder ios => Objective C Language Screenshot 2022-09-16 at 12 20 59 AM

Same issue

sacmii avatar Sep 15 '22 18:09 sacmii

Mentioned in https://github.com/facebook/react-native/issues/34673#issuecomment-1245377640

baylesa-dev avatar Sep 15 '22 18:09 baylesa-dev

Same here, any more info we can provide to help patch this?

Or is there a temp workaround?

ArturoTorresMartinez avatar Sep 15 '22 23:09 ArturoTorresMartinez

So far, nothing I'm aware of. I was trying to patch Podfile with the solutions mentioned in the linked issue, and the app is compiling indeed, but vision camera plugins are not working.

We need to wait for a patch from react-native. To run your app smoothly, you need to downgrade Xcode to version 13.4.

jpudysz avatar Sep 16 '22 07:09 jpudysz

So far, nothing I'm aware of. I was trying to patch Podfile with the solutions mentioned in the linked issue, and the app is compiling indeed, but vision camera plugins are not working.

We need to wait for a patch from react-native. To run your app smoothly, you need to downgrade Xcode to version 13.4.

Thanks @jpudysz , I can confirm downgrading to version 13.4 solves the issue for now.

I'll also keep this thread updated if I hear any updates or workarounds.

Thanks! 👍

ArturoTorresMartinez avatar Sep 16 '22 13:09 ArturoTorresMartinez

I have this issue after upgrading RN to 0.70.1 My XCode version is 13.4.1.

I made a repo to reproduce the issue. https://github.com/bglgwyng/FrameProcessorExample2

In my case, scanCodes from vision-camera-code-scanner works fine. However, my custom empty frame processor xyz that is scaffolled with vision-camera-plugin-builder crashes the app with the same error message here.

bglgwyng avatar Sep 18 '22 09:09 bglgwyng

So far, nothing I'm aware of. I was trying to patch Podfile with the solutions mentioned in the linked issue, and the app is compiling indeed, but vision camera plugins are not working.

We need to wait for a patch from react-native. To run your app smoothly, you need to downgrade Xcode to version 13.4.

Thanks, I confirm that this solution works, for me is just a temporary workaround, because I need to deploy to the latest iOS, so I had to downgrade to Xcode 13.4 and then add support for iOS 16.0

Hope this will be fixed also for the latest Xcode version

In this thread you can find, how to add support

https://developer.apple.com/forums/thread/673131

image

Neklan avatar Sep 22 '22 08:09 Neklan

VISION_EXPORT_SWIFT_FRAME_PROCESSOR is working on my main Project, but not working in pod project like vision-camera-code-scanner when build by Xcode 14.0.

If I chagne this code __attribute__((constructor)) static void VISION_CONCAT(initialize_, objc_name)() to +(void)load

then it work (temporary). I tested it.

intmain avatar Sep 28 '22 19:09 intmain

VISION_EXPORT_SWIFT_FRAME_PROCESSOR is working on my main Project, but not working in pod project like vision-camera-code-scanner when build by Xcode 14.0.

If I chagne this code __attribute__((constructor)) static void VISION_CONCAT(initialize_, objc_name)() to +(void)load

then it work (temporary). I tested it.

Thanks @intmain , it fixed the issue for me temporarily too. It seems that using attribute((constructor)) no longer works with load() and not sure why swift bridge to objc works (using +(void)load )

(I'm not familiar with swift, so if I was wrong with something, please correct me.)

EasonLex avatar Sep 29 '22 03:09 EasonLex

VISION_EXPORT_SWIFT_FRAME_PROCESSOR is working on my main Project, but not working in pod project like vision-> camera-code-scanner when build by Xcode 14.0.

If I chagne this code attribute((constructor)) static void VISION_CONCAT(initialize_, objc_name)() to +(void)load

then it work (temporary). I tested it.

Can you please provide more information on how to modify the code and where, so we can apply it also? Thank you

Neklan avatar Sep 30 '22 10:09 Neklan

@Neklan You can apply that patch with the following patch script and patch-package,

diff --git a/node_modules/react-native-vision-camera/ios/Frame Processor/FrameProcessorPlugin.h b/node_modules/react-native-vision-camera/ios/Frame Processor/FrameProcessorPlugin.h
index a2ccdcb4..7c9690e0 100644
--- a/node_modules/react-native-vision-camera/ios/Frame Processor/FrameProcessorPlugin.h	
+++ b/node_modules/react-native-vision-camera/ios/Frame Processor/FrameProcessorPlugin.h	
@@ -53,7 +53,7 @@ objc_name : NSObject<FrameProcessorPluginBase>
 @end                                                                                \
 @implementation objc_name (FrameProcessorPlugin)                                    \
                                                                                     \
-__attribute__((constructor)) static void VISION_CONCAT(initialize_, objc_name)()    \
++(void)load                                                                         \
 {                                                                                   \
   [FrameProcessorPluginRegistry addFrameProcessorPlugin:@"__" @ #name callback:^id(Frame* frame, NSArray<id>* args) {    \
     return [objc_name callback:frame withArgs:args];                               \

Sadly, it didn't fix my problem with custom frame processor https://github.com/mrousavy/react-native-vision-camera/issues/1244#issuecomment-1250233793. Or those problems are irrelevant to each other.

bglgwyng avatar Oct 01 '22 14:10 bglgwyng

VISION_EXPORT_SWIFT_FRAME_PROCESSOR is working on my main Project, but not working in pod project like vision-camera-code-scanner when build by Xcode 14.0.

If I chagne this code __attribute__((constructor)) static void VISION_CONCAT(initialize_, objc_name)() to +(void)load

then it work (temporary). I tested it.

Thanks man, issue fixed too.

zzz08900 avatar Oct 01 '22 18:10 zzz08900

@Neklan You can apply that patch with the following patch script and patch-package,

diff --git a/node_modules/react-native-vision-camera/ios/Frame Processor/FrameProcessorPlugin.h b/node_modules/react-native-vision-camera/ios/Frame Processor/FrameProcessorPlugin.h
index a2ccdcb4..7c9690e0 100644
--- a/node_modules/react-native-vision-camera/ios/Frame Processor/FrameProcessorPlugin.h	
+++ b/node_modules/react-native-vision-camera/ios/Frame Processor/FrameProcessorPlugin.h	
@@ -53,7 +53,7 @@ objc_name : NSObject<FrameProcessorPluginBase>
 @end                                                                                \
 @implementation objc_name (FrameProcessorPlugin)                                    \
                                                                                     \
-__attribute__((constructor)) static void VISION_CONCAT(initialize_, objc_name)()    \
++(void)load                                                                         \
 {                                                                                   \
   [FrameProcessorPluginRegistry addFrameProcessorPlugin:@"__" @ #name callback:^id(Frame* frame, NSArray<id>* args) {    \
     return [objc_name callback:frame withArgs:args];                               \

Sadly, it didn't fix my problem with custom frame processor #1244 (comment). Or those problems are irrelevant to each other.

The method posted by @intmain only solves crashes caused by upgrading to Xcode 14 - which means if you're having problems with Xcode 13.x, then the problems are probably elsewhere.

Hmm, I took a look at your frame processor and it seems you didn't add any code besides the example in doc. If that is the case then I suggest you open a separate issue and ask for help there.

zzz08900 avatar Oct 01 '22 18:10 zzz08900

I confirm, that solution posted above with changing __attribute__((constructor)) static void VISION_CONCAT(initialize_, objc_name)() to +(void)load is working on the newest xCode 14.0.1 with custom frame processor

Neklan avatar Oct 03 '22 08:10 Neklan

@Neklan You can apply that patch with the following patch script and patch-package,

diff --git a/node_modules/react-native-vision-camera/ios/Frame Processor/FrameProcessorPlugin.h b/node_modules/react-native-vision-camera/ios/Frame Processor/FrameProcessorPlugin.h
index a2ccdcb4..7c9690e0 100644
--- a/node_modules/react-native-vision-camera/ios/Frame Processor/FrameProcessorPlugin.h	
+++ b/node_modules/react-native-vision-camera/ios/Frame Processor/FrameProcessorPlugin.h	
@@ -53,7 +53,7 @@ objc_name : NSObject<FrameProcessorPluginBase>
 @end                                                                                \
 @implementation objc_name (FrameProcessorPlugin)                                    \
                                                                                     \
-__attribute__((constructor)) static void VISION_CONCAT(initialize_, objc_name)()    \
++(void)load                                                                         \
 {                                                                                   \
   [FrameProcessorPluginRegistry addFrameProcessorPlugin:@"__" @ #name callback:^id(Frame* frame, NSArray<id>* args) {    \
     return [objc_name callback:frame withArgs:args];                               \

Sadly, it didn't fix my problem with custom frame processor #1244 (comment). Or those problems are irrelevant to each other.

this fixed the issue for me as well

alexco2 avatar Oct 03 '22 10:10 alexco2

Same issue in android, please help me . Have switched back to react native camera which is deprecated.

le-blip avatar Oct 10 '22 10:10 le-blip

@Neklan You can apply that patch with the following patch script and patch-package,

diff --git a/node_modules/react-native-vision-camera/ios/Frame Processor/FrameProcessorPlugin.h b/node_modules/react-native-vision-camera/ios/Frame Processor/FrameProcessorPlugin.h
index a2ccdcb4..7c9690e0 100644
--- a/node_modules/react-native-vision-camera/ios/Frame Processor/FrameProcessorPlugin.h	
+++ b/node_modules/react-native-vision-camera/ios/Frame Processor/FrameProcessorPlugin.h	
@@ -53,7 +53,7 @@ objc_name : NSObject<FrameProcessorPluginBase>
 @end                                                                                \
 @implementation objc_name (FrameProcessorPlugin)                                    \
                                                                                     \
-__attribute__((constructor)) static void VISION_CONCAT(initialize_, objc_name)()    \
++(void)load                                                                         \
 {                                                                                   \
   [FrameProcessorPluginRegistry addFrameProcessorPlugin:@"__" @ #name callback:^id(Frame* frame, NSArray<id>* args) {    \
     return [objc_name callback:frame withArgs:args];                               \

Sadly, it didn't fix my problem with custom frame processor #1244 (comment). Or those problems are irrelevant to each other.

how can i make and use that script

ikuruzum avatar Oct 10 '22 14:10 ikuruzum

@ikuruzum all you need to do is googling "how to use patch-package"

zzz08900 avatar Oct 11 '22 07:10 zzz08900

Same issue in android, please help me . Have switched back to react native camera which is deprecated.

This issue has nothing to do with android, you're facing a different problem

zzz08900 avatar Oct 11 '22 07:10 zzz08900

I confirm that using the patch above works with Xcode 14.0.1 :)

I had no problem when using Objective-C for the iOS native module, but as soon as you use Swift, you need to apply that patch.

qtipee avatar Oct 18 '22 07:10 qtipee

@mrousavy can you please look at this? Seems to work fine.

LazyAfternoons avatar Oct 19 '22 20:10 LazyAfternoons

Huh, that's interesting. I'll take a look to find out if that's safe to do

mrousavy avatar Oct 20 '22 11:10 mrousavy

Could you create a PR with the change?

mrousavy avatar Oct 20 '22 11:10 mrousavy