capacitor-community / barcode-scanner

A fast and efficient (QR) barcode scanner for Capacitor
MIT License
437 stars 172 forks source link

iOS: Crash when scanner is quickly started and stopped repeatedly #125

Closed bc-m closed 2 years ago

bc-m commented 2 years ago

Describe the bug

The plugin crashes our app when the scanner is quickly started and stopped repeatedly. In my tests, it is enough to open and close the scanner 2-3x quickly to reproduce the problem.

⚡️  To Native ->  BarcodeScanner checkPermission 34902191
⚡️  TO JS {"granted":true}
⚡️  To Native ->  BarcodeScanner hideBackground 34902192
⚡️  TO JS undefined
⚡️  To Native ->  BarcodeScanner startScan 34902193
AVMetadataObjectType(_rawValue: org.iso.QRCode)
⚡️  To Native ->  BarcodeScanner showBackground 34902194
⚡️  TO JS undefined
⚡️  To Native ->  BarcodeScanner stopScan 34902195
⚡️  TO JS undefined
⚡️  To Native ->  BarcodeScanner showBackground 34902196
⚡️  To Native ->  BarcodeScanner stopScan 34902197
⚡️  TO JS undefined
⚡️  TO JS undefined
⚡️  To Native ->  BarcodeScanner checkPermission 34902198
⚡️  TO JS {"granted":true}
⚡️  To Native ->  BarcodeScanner hideBackground 34902199
⚡️  TO JS ⚡️  To Native ->  BarcodeScanner startScan 34902200
undefined
AVMetadataObjectType(_rawValue: org.iso.QRCode)
⚡️  To Native ->  BarcodeScanner showBackground 34902201
⚡️  To Native ->  BarcodeScanner stopScan 34902202
⚡️  To Native ->  BarcodeScanner showBackground 34902203
⚡️  To Native ->  BarcodeScanner stopScan 34902204
⚡️  TO JS undefined
⚡️  TO JS undefined
⚡️  TO JS undefined
⚡️  TO JS undefined
CapacitorCommunityBarcodeScanner/Plugin.swift:225: Fatal error: Unexpectedly found nil while unwrapping an Optional value
2022-05-20 08:35:23.042591+0200 App[13242:899923] CapacitorCommunityBarcodeScanner/Plugin.swift:225: Fatal error: Unexpectedly found nil while unwrapping an Optional value

To Reproduce Steps to reproduce the behavior:

  1. Open barcode scanner and close it repeatly
  2. See error
CapacitorCommunityBarcodeScanner/Plugin.swift:225: Fatal error: Unexpectedly found nil while unwrapping an Optional value
2022-05-20 08:35:23.042591+0200 App[13242:899923] CapacitorCommunityBarcodeScanner/Plugin.swift:225: Fatal error: Unexpectedly found nil while unwrapping an Optional value

Expected behavior

Not to crash.

Version

v2.1.1

Smartphone (please complete the following information):

thegnuu commented 2 years ago

Sorry for the delayed answer. I was not able to test it on iOS 15.5 right now, had to test something on a prior iOS version and I have just one physical device.

I will update my iPhone right now and will give you some feedback as soon as I was able to test it.

bc-m commented 2 years ago

As can be seen in the log, "stopScan" was executed twice at the same time. This was caused by incorrect use of "React.useEffect" in our application. We have now fixed the double execution of "stopScan", which prevents the application from crashing.

You can close this ticket if you want, as the problem was caused by improper use.

thegnuu commented 2 years ago

Thank you for the update! That's what I thought after testing it on my device and everything worked ;)

Cheers!

alexbainbridge commented 2 years ago

I have this same issue now, same error message. I can recreate it constantly by

1) Do a scan, with a QR code, and everything works as you expect 2) Do a scan, tap cancel button (which we added to the UI), which calls stop scan - this crashes

Temporarily moved to pauseScanning rather than stopScan and that doesn't fail.

thegnuu commented 2 years ago

@alexbainbridge unfortunately I am still not able to reproduce it in my test environments. Are you able to share your repo so I can have a look at it?

alexbainbridge commented 2 years ago

I can't share my repo (as its a live app) but could try to extract just this part into a new repo. As mentioned, moved to pausing scanning rather than stopping scanning, so this is not a live concern for me today (but was until I worked out how to get around the issue). i.e. this might be some time before I make this repo

alexbainbridge commented 1 year ago

3.0.1 has fixed it, FYI