Luis
Luis

Reputation: 1009

Xcode 9 & Swift 4 - Unexpected Crash When Presenting View Controller

I am getting a crash only when using Xcode 9 Beta 1 and Swift 4. I am trying to present a view controller but an exception is thrown, even though I have several breakpoints to catch all exceptions Xcode doesn't stop anywhere.

Using lldb I was able to print the exception since Xcode didn't do it. Here is what I got:

(lldb) po $arg1

-[UIDeviceRGBColor pointSize]: unrecognized selector sent to instance 0x60000067a6c0
(null)

Here is the stack trace:

enter image description here

And the backtrace:

(lldb) bt
* thread #1, queue = 'com.apple.main-thread', stop reason = breakpoint 3.1 5.1
  * frame #0: 0x00000001090e60f1 libobjc.A.dylib`objc_exception_throw
    frame #1: 0x0000000109806494 CoreFoundation`-[NSObject(NSObject) doesNotRecognizeSelector:] + 132
    frame #2: 0x00000001096ff858 CoreFoundation`___forwarding___ + 1432
    frame #3: 0x00000001096ff238 CoreFoundation`__forwarding_prep_0___ + 120
    frame #4: 0x0000000112e9f350 UIFoundation`__NSStringDrawingEngine + 3564
    frame #5: 0x0000000112ea32b2 UIFoundation`-[NSAttributedString(NSExtendedStringDrawing) boundingRectWithSize:options:context:] + 787
    frame #6: 0x000000010a84e96e UIKit`-[UILabel _textRectForBounds:limitedToNumberOfLines:includingShadow:] + 1022
    frame #7: 0x000000010a84e33b UIKit`-[UILabel textRectForBounds:limitedToNumberOfLines:] + 68
    frame #8: 0x000000010a8543c9 UIKit`-[UILabel _intrinsicSizeWithinSize:] + 167
    frame #9: 0x000000010b242b24 UIKit`-[UITextField _intrinsicSizeWithinSize:] + 238
    frame #10: 0x000000010b0b61ab UIKit`-[UIView(UIConstraintBasedLayout) intrinsicContentSize] + 37
    frame #11: 0x000000010b0b6a00 UIKit`-[UIView(UIConstraintBasedLayout) _generateContentSizeConstraints] + 35
    frame #12: 0x000000010b0b6643 UIKit`-[UIView(UIConstraintBasedLayout) _updateContentSizeConstraints] + 262
    frame #13: 0x000000010b0c0fef UIKit`-[UIView(AdditionalLayoutSupport) _updateSystemConstraints] + 99
    frame #14: 0x000000010b0bf8b7 UIKit`-[UIView(AdditionalLayoutSupport) _sendUpdateConstraintsIfNecessaryForSecondPass:] + 161
    frame #15: 0x000000010b0bfebf UIKit`-[UIView(AdditionalLayoutSupport) _updateConstraintsIfNeededCollectingViews:forSecondPass:] + 1286
    frame #16: 0x000000010b0bfd3e UIKit`-[UIView(AdditionalLayoutSupport) _updateConstraintsIfNeededCollectingViews:forSecondPass:] + 901
    frame #17: 0x000000010b0bfd3e UIKit`-[UIView(AdditionalLayoutSupport) _updateConstraintsIfNeededCollectingViews:forSecondPass:] + 901
    frame #18: 0x000000010b0bfd3e UIKit`-[UIView(AdditionalLayoutSupport) _updateConstraintsIfNeededCollectingViews:forSecondPass:] + 901
    frame #19: 0x000000010b0bfd3e UIKit`-[UIView(AdditionalLayoutSupport) _updateConstraintsIfNeededCollectingViews:forSecondPass:] + 901
    frame #20: 0x0000000107d1d1a1 Foundation`-[NSISEngine withBehaviors:performModifications:] + 131
    frame #21: 0x000000010b0c06f0 UIKit`__100-[UIView(AdditionalLayoutSupport) _updateConstraintsIfNeededWithViewForVariableChangeNotifications:]_block_invoke + 90
    frame #22: 0x000000010b0bef7e UIKit`-[UIView(AdditionalLayoutSupport) _withUnsatisfiableConstraintsLoggingSuspendedIfEngineDelegateExists:] + 104
    frame #23: 0x000000010b0c025f UIKit`-[UIView(AdditionalLayoutSupport) _updateConstraintsIfNeededWithViewForVariableChangeNotifications:] + 160
    frame #24: 0x000000010b0c134f UIKit`-[UIView(AdditionalLayoutSupport) _updateConstraintsAtEngineLevelIfNeededWithViewForVariableChangeNotifications:] + 344
    frame #25: 0x000000010a66f414 UIKit`-[UIView(Hierarchy) _updateConstraintsAsNecessaryAndApplyLayoutFromEngine] + 159
    frame #26: 0x000000010a599e09 UIKit`-[UIPickerView layoutSubviews] + 189
    frame #27: 0x000000010a59d697 UIKit`-[UIPickerView selectedRowInComponent:] + 45
    frame #28: 0x000000010a5982fc UIKit`-[UIPickerView _updateSelectedRows] + 143
    frame #29: 0x000000010a598396 UIKit`-[UIPickerView didMoveToWindow] + 101
    frame #30: 0x000000010a67a8e1 UIKit`-[UIView(Internal) _didMoveFromWindow:toWindow:] + 1701
    frame #31: 0x000000010a67a530 UIKit`-[UIView(Internal) _didMoveFromWindow:toWindow:] + 756
    frame #32: 0x000000010a67a530 UIKit`-[UIView(Internal) _didMoveFromWindow:toWindow:] + 756
    frame #33: 0x000000010a692e45 UIKit`-[UIScrollView _didMoveFromWindow:toWindow:] + 84
    frame #34: 0x000000010a67a530 UIKit`-[UIView(Internal) _didMoveFromWindow:toWindow:] + 756
    frame #35: 0x000000010a66d259 UIKit`__45-[UIView(Hierarchy) _postMovedFromSuperview:]_block_invoke + 151
    frame #36: 0x000000010a66d140 UIKit`-[UIView(Hierarchy) _postMovedFromSuperview:] + 828
    frame #37: 0x000000010a67d4ad UIKit`-[UIView(Internal) _addSubview:positioned:relativeTo:] + 1917
    frame #38: 0x000000010a71a82b UIKit`-[UITransitionView transition:fromView:toView:removeFromView:] + 1541
    frame #39: 0x000000010b2c9f29 UIKit`-[UIViewControllerBuiltinTransitionViewAnimator animateTransition:] + 1712
    frame #40: 0x000000010a720845 UIKit`__56-[UIPresentationController runTransitionForCurrentState]_block_invoke + 3003
    frame #41: 0x000000010a5c46cc UIKit`_runAfterCACommitDeferredBlocks + 318
    frame #42: 0x000000010a5b2cef UIKit`_cleanUpAfterCAFlushAndRunDeferredBlocks + 280
    frame #43: 0x000000010a5e3091 UIKit`_afterCACommitHandler + 137
    frame #44: 0x000000010971f507 CoreFoundation`__CFRUNLOOP_IS_CALLING_OUT_TO_AN_OBSERVER_CALLBACK_FUNCTION__ + 23
    frame #45: 0x000000010971f45e CoreFoundation`__CFRunLoopDoObservers + 430
    frame #46: 0x00000001097038f4 CoreFoundation`__CFRunLoopRun + 1332
    frame #47: 0x0000000109703149 CoreFoundation`CFRunLoopRunSpecific + 409
    frame #48: 0x000000011182c9d7 GraphicsServices`GSEventRunModal + 62
    frame #49: 0x000000010a5b891d UIKit`UIApplicationMain + 159
    frame #50: 0x00000001076a1cc7 GradePoint`main at AppDelegate.swift:13
    frame #51: 0x000000010d224b65 libdyld.dylib`start + 1

I understand it has to do with an unrecognized selector, but I am not doing anything with UIDeviceRGBColor or PointSize.

Not really sure what could be going on, this all works just fine when I switch over to Xcode 8.

If you would like to reproduce feel free to clone the project here and switch to the features/ios11 branch. All you have to do is finish the intro screen, and click the + button. The crash will happen instantly afterward. Run using Xcode 9 Beta 1

I understand this just might be a beta bug, but if anyone has more experience with this I would appreciate some help, if not, at least help with how to file an Xcode bug report since I've never done one.

Thanks!

Upvotes: 0

Views: 1861

Answers (2)

Sulthan
Sulthan

Reputation: 130072

UIDeviceRGBColor is technically a UIColor. pointSize is a method on UIFont. The bug happens when performing layout of a UILabel.

The most probable cause is that you are assigning a UIColor instance to a UILabel.font.

If you have an attributed string, it would be caused e.g. by attributes:

let attributes: [String: Any] = [NSFontAttributeName: UIColor.black]

Note that since attributes take Any values, there is no type checking and this can happen in Swift.

Upvotes: 3

Mayur
Mayur

Reputation: 33

I tried downloading your code and reproducing the crash but I'm not able to see a crash. I've tried running your code on the iPhone 7 Plus simulator and on my iPhone 6S and it works fine both places. (I'm running your project on Xcode 9 Beta 1)

Could you share any more details on the environment you're running your code in? Perhaps the Swift version setting you're using (Select your target > Build Settings > Swift Language Version) or whether you've performed a Swift 4 migration? I'll be happy to look into it further.

Meanwhile, here's how to file a bug report with Apple.

[I know this content should be posted as a comment rather than an answer but I don't have enough reputation points to do that at the time of writing.]

Upvotes: 2

Related Questions