0

I'm making a macOS app and I've got this code used to rotate an image:

didSet {
        let degreesToRotate = oldValue - phoneXRotation
        phoneImageView.rotate(byDegrees: CGFloat(degreesToRotate))
        degreesLabel.isHidden = false
        degreesLabel.stringValue = "\(phoneXRotation)"
        if phoneXRotation > 70 {
            statusLabel.stringValue = "Phone orientation: Right Tilt"
        } else if phoneXRotation < -70 {
            statusLabel.stringValue = "Phone orientation: Left Tilt"
        } else {
            statusLabel.stringValue = "Phone orientation: Flat"
        }
    }

The app will randomly crash in an lldb error. If I comment out the third line that rotate the phone, I have no problems. The conversion from Int to CGFloat shouldn't crash. Any ideas?

Here is the stack trace:

* thread #1, queue = 'com.apple.main-thread', stop reason = EXC_BAD_INSTRUCTION (code=EXC_I386_INVOP, subcode=0x0)
frame #0: 0x00007fff8989b44e AppKit`-[NSApplication _crashOnException:] + 109
frame #1: 0x00007fff8996ea32 AppKit`__37+[NSDisplayCycle currentDisplayCycle]_block_invoke.31 + 708
frame #2: 0x00007fff8ba62d37 CoreFoundation`__CFRUNLOOP_IS_CALLING_OUT_TO_AN_OBSERVER_CALLBACK_FUNCTION__ + 23
frame #3: 0x00007fff8ba62ca7 CoreFoundation`__CFRunLoopDoObservers + 391
frame #4: 0x00007fff8ba436d9 CoreFoundation`__CFRunLoopRun + 873
frame #5: 0x00007fff8ba43114 CoreFoundation`CFRunLoopRunSpecific + 420
frame #6: 0x00007fff8afa3ebc HIToolbox`RunCurrentEventLoopInMode + 240
frame #7: 0x00007fff8afa3cf1 HIToolbox`ReceiveNextEventCommon + 432
frame #8: 0x00007fff8afa3b26 HIToolbox`_BlockUntilNextEventMatchingListInModeWithFilter + 71
frame #9: 0x00007fff8953ca54 AppKit`_DPSNextEvent + 1120
frame #10: 0x00007fff89cb87ee AppKit`-[NSApplication(NSEvent) _nextEventMatchingEventMask:untilDate:inMode:dequeue:] + 2796
frame #11: 0x00007fff895313db AppKit`-[NSApplication run] + 926
frame #12: 0x00007fff894fbe0e AppKit`NSApplicationMain + 1237
* frame #13: 0x000000010000691d Dataspeed Mac Project`main at AppDelegate.swift:12
frame #14: 0x00007fffa11f1235 libdyld.dylib`start + 1
frame #15: 0x00007fffa11f1235 libdyld.dylib`start + 1
A Tyshka
  • 3,830
  • 7
  • 24
  • 46
  • What does the crashed stack look like? (And any console error message.) – Phillip Mills Aug 29 '17 at 15:17
  • @PhillipMills No console error message, just `lldb` – A Tyshka Aug 29 '17 at 15:18
  • What thread is this occurring on? Do you possibly have any code that might try to set this property from a background thread? – Charles Srstka Aug 29 '17 at 15:19
  • @CharlesSrstka the value of this is set by core bluetooth. However, the value of the property shouldn't matter since `oldValue - phoneXRotation` runs just fine without crashing. It's only when I pass that value into `rotate()` that I have issues – A Tyshka Aug 29 '17 at 15:24
  • @ATyshka - OK, show the stack trace of the crash. – Phillip Mills Aug 29 '17 at 15:24
  • @PhillipMills How do I do that? – A Tyshka Aug 29 '17 at 15:27
  • @PhillipMills I added the stacked trace – A Tyshka Aug 29 '17 at 16:02
  • From the stack trace, it looks like the crash is happening in the middle of the display cycle. Perhaps iOS doesn't like you altering the orientation in the middle of its display process. Try delaying the operation until the next run of the event loop by wrapping the whole `didSet` block in a `DispatchQueue.main.async` block and see if that fixes it. – Charles Srstka Aug 29 '17 at 16:34

0 Answers0