twostraws / CodeScanner

A SwiftUI view that is able to scan barcodes, QR codes, and more, and send back what was found.
MIT License
1.02k stars 293 forks source link

CodeScanner crashes sometimes #18

Closed jojost1 closed 3 years ago

jojost1 commented 4 years ago

Hi,

I'm noticing some crashes caused when users tap the button that starts the CodeScannerView.

if AVCaptureDevice.authorizationStatus(for: .video) == .authorized {
    CodeScannerView(codeTypes: [.qr], simulatedData: "testtest", completion: self.handleScan)
} else {
    Text("Please give permission to access the camera in the pop-up or in Settings > Privacy > Camera.")
        .font(.headline)
        .multilineTextAlignment(.center)
        .padding(24)
        .onAppear(perform: {
            AVCaptureDevice.requestAccess(for: .video, completionHandler: { value in
                self.isShowingSheet = false
                self.isShowingSheet = true
            })
        })
 }

An error log of one of my TestFlight users:

Exception Type:  EXC_CRASH (SIGABRT)
Exception Codes: 0x0000000000000000, 0x0000000000000000
Exception Note:  EXC_CORPSE_NOTIFY
Triggered by Thread:  0

Last Exception Backtrace:
0   CoreFoundation                  0x184094360 0x183f7b000 + 1151840
1   libobjc.A.dylib                 0x183db7090 0x183db1000 + 24720
2   CoreFoundation                  0x183f9dce4 0x183f7b000 + 142564
3   libswiftCore.dylib              0x193283338 0x193271000 + 74552
4   Pi-hole Remote                  0x104b398e8 specialized _ArrayBuffer._getElementSlowPath(_:) + 68
5   Pi-hole Remote                  0x104b38d74 CodeScannerView.ScannerViewController.updateOrientation() + 616 (CodeScanner.swift:0)
6   Pi-hole Remote                  0x104b38da0 @objc CodeScannerView.ScannerViewController.updateOrientation() + 28 (<compiler-generated>:0)
7   CoreFoundation                  0x183ff7b68 0x183f7b000 + 510824
8   CoreFoundation                  0x183ff7b28 0x183f7b000 + 510760
9   CoreFoundation                  0x183ff7120 0x183f7b000 + 508192
10  CoreFoundation                  0x183ff6b18 0x183f7b000 + 506648
11  Foundation                      0x1843a0ba0 0x18439a000 + 27552
12  UIKitCore                       0x18873c2d8 0x187c38000 + 11551448
13  UIKitCore                       0x187e3edf0 0x187c38000 + 2125296
14  UIKitCore                       0x18826d598 0x187c38000 + 6509976
15  UIKitCore                       0x1883690d0 0x187c38000 + 7540944
16  UIKitCore                       0x187e3ebc4 0x187c38000 + 2124740
17  UIKitCore                       0x187c84db8 0x187c38000 + 314808
18  UIKitCore                       0x187c83858 0x187c38000 + 309336
19  UIKitCore                       0x187c84a04 0x187c38000 + 313860
20  UIKitCore                       0x188293ca4 0x187c38000 + 6667428
21  FrontBoardServices              0x189a9a3e4 0x189a90000 + 41956
22  FrontBoardServices              0x189ac2b24 0x189a90000 + 207652
23  FrontBoardServices              0x189aa7200 0x189a90000 + 94720
24  FrontBoardServices              0x189ac2a70 0x189a90000 + 207472
25  libdispatch.dylib               0x183d54560 0x183cf4000 + 394592
26  libdispatch.dylib               0x183cfa0e0 0x183cf4000 + 24800
27  FrontBoardServices              0x189ae6cc0 0x189a90000 + 355520
28  FrontBoardServices              0x189ae6988 0x189a90000 + 354696
29  FrontBoardServices              0x189ae6e5c 0x189a90000 + 355932
30  CoreFoundation                  0x18401437c 0x183f7b000 + 627580
31  CoreFoundation                  0x1840142c4 0x183f7b000 + 627396
32  CoreFoundation                  0x184013920 0x183f7b000 + 624928
33  CoreFoundation                  0x18400e2e0 0x183f7b000 + 602848
34  CoreFoundation                  0x18400dbc0 0x183f7b000 + 601024
35  GraphicsServices                0x18fc37598 0x18fc34000 + 13720
36  UIKitCore                       0x188741240 0x187c38000 + 11571776
37  UIKitCore                       0x18874695c 0x187c38000 + 11594076
38  Pi-hole Remote                  0x104873b4c main + 68 (ActiveInstance.swift:39)
39  libdyld.dylib                   0x183ea25b4 0x183ea1000 + 5556

Any idea what's going on? Perhaps something with @objc CodeScannerView.ScannerViewController.updateOrientation()?

twostraws commented 4 years ago

Possibly – perhaps set a breakpoint and see what the problem is, then open a PR if you can find a fix.

btian commented 4 years ago

It also crashes for me at the same place.

2020-08-04 17:07:21.719691-0700 essen[24474:6831321] *** Terminating app due to uncaught exception 'NSRangeException', reason: '*** -[__NSSingleObjectArrayI objectAtIndex:]: index 1 beyond bounds [0 .. 0]'
*** First throw call stack:
(0x19375d654 0x19347fbcc 0x19365a230 0x1a0c0e1e4 0x1a0c11a4c 0x104aa5654 0x104aa57dc 0x1936b9504 0x1936b954c 0x1936b88a4 0x1936b857c 0x1936352d4 0x1936b7edc 0x193a25974 0x1974805c0 0x196fb65f8 0x1973882ac 0x19747d62c 0x196fb652c 0x196fb6430 0x19747d528 0x196fb62c8 0x196e27e30 0x196e269d0 0x196e27b68 0x1973ada08 0x1988e50a8 0x198909684 0x1988eebfc 0x1989095b8 0x1073cf730 0x1073d2bb0 0x19892d850 0x19892d51c 0x1989127cc 0x1988dd790 0x197809f48 0x197854cb4 0x19a1bbc54 0x19a1e54e4 0x19783a9d0 0x1936daf2c 0x1936d5e20 0x1936d629c 0x1936d5ba8 0x19d845344 0x1978113e4 0x1049d3e2c 0x19355d8f0)
libc++abi.dylib: terminating with uncaught exception of type NSException
btian commented 4 years ago

I don't know how to fix it though. What I did in my code base is to just remove updateOrientation() function.

jojost1 commented 4 years ago

Ended up switching to this: https://github.com/heart/CarBode-Barcode-Scanner-For-SwiftUI

Seems to be running without crashes & is maintained.

danielsaidi commented 4 years ago

@twostraws It's an array out of bounds crash, since the connections array is incorrectly accessed:

@objc func updateOrientation() {
    guard let orientation = UIApplication.shared.windows.first?.windowScene?.interfaceOrientation else {
        return
    }
    let previewConnection = captureSession.connections[1] // UH OH
    previewConnection.videoOrientation = AVCaptureVideoOrientation(rawValue: orientation.rawValue) ?? .portrait
}

However, it's fixed in master. Perhaps consider releasing a new version?

dscoppelletti commented 3 years ago

In iOS 14 I have the same problem all the time; I have never see it in iOS 13 (maybe lucky?). However, if it is already fixed in master, a new release would be appreciated.

dscoppelletti commented 3 years ago

Same problem in iOS 14.0.1: is this project still active?

jzeisweiss commented 3 years ago

This was fixed and just needs a new release: https://github.com/twostraws/CodeScanner/pull/10

twostraws commented 3 years ago

This ought to be resolved.