I'm starting to integrate Swift code into my Obj-C project. Everything's been going smoothly but today, when I updated to Xcode 6.1, things hit the fan. I'm getting a number of "trap" exceptions from Swift code that was previously running fine.
The first crash is in my extension of UIFont. Here's my code:
class func specialFont() -> UIFont {
let font = UIFont(name: "FontName", size: 12.0)
return font!
}
This code was working fine before updating to Xcode 6.1, but now crashes with EXC_BREAKPOINT (code=EXC_ARM_BREAKPOINT), at dylddyld_fatal_error: 0x1fe1608c: trap. (BTW, I don't have an exception breakpoint set.) Attempting to continue program execution doesn't generate a crash log, it just enters an endless cycle of generating that exception. Again, code worked fine before Xcode 6.1 update.
The second crash is especially strange because it only happens when debugging on a device, not on the simulator. Same exception info as above. Here's the second bit of code that's crashing:
let tap = UITapGestureRecognizer(target: self, action: "didTap")
self.addGestureRecognizer(tap)
Thanks for any help!
UPDATE
Stack trace is below:
0] dyld_fatal_error
1] dyld_stub_binder
4] Project.FileWhereCrashTriggers.methodTriggeringCrash
5] #objc Project.FileWhereCrashTriggers.methodTriggeringCrash
6] -- continued trace up to call of method crashing
One weird thing about these crashes is that they don't trigger the exception on the actual line of code, as you can see from the stack trace. They break in dyld_fatal_error.
I can't upload any console output because the app never actually crashes while running in Xcode - it calls endlessly similar to EXC_BAD_ACCESS. I triggered the crash in non-debug mode and then looked at the device logs using Organizer and this is what it offered:
Exception Type: EXC_BREAKPOINT (SIGTRAP)
Exception Codes: 0x0000000000000001, 0x00000000e7ffdefe
Triggered by Thread: 0
Dyld Error Message:
Symbol not found: __TFSSCfMSSFT21_builtinStringLiteralBp8byteSizeBw7isASCIIBi1__SS
Referenced from: /private/var/mobile/Containers/Bundle/Application/4ABA71F8-A6D1-4E4B-A452-DE339065310A/Project.app/Project
Expected in: /private/var/mobile/Containers/Bundle/Application/4ABA71F8-A6D1-4E4B-A452-DE339065310A/Project.app/Frameworks/libswiftCore.dylib
Dyld Version: 353.5
A clean build solved it for me (clean from XCode and delete app on device)
After experimenting more, I was able to resolved these conflicts by using the #objc marker in my swift files in all swift methods that could get called by my Obj-C code, including init methods. For example, change override init(frame: CGRect) to #objc override init(frame: CGRect).
As an aside: I'm still a bit confused by why the #objc flag wasn't 'required' (although it may have been best practice) in Xcode 6.0 but suddenly was a requirement in Xcode 6.1.
Related
Using Xcode 12.4 Simulator 14.4
For the alert displayed below, I have the following function in my XCUITests that I use to pull the static text displayed on the System Alert:
_ = addUIInterruptionMonitor(withDescription: "", handler: { (alert) -> Bool in
var data = alert.staticTexts.allElementsBoundByIndex.map {$0.label}
...
})
This worked when using Xcode 11.3 Sims 13.3; however, I just upgraded Xcode and now it crashes on when running the map function and gives the following error:
error: Execution was interrupted, reason: internal ObjC exception breakpoint(-8)..The process has been returned to the state before expression evaluation.
If I put a breakpoint, I can see the following:
po alert.staticTexts.allElementsBoundByIndex.count -> returns 3
po alert.staticTexts.allElementsBoundByIndex[0].label -> returns "Allow “X” to use your location?"
po alert.staticTexts.allElementsBoundByIndex[1].label -> returns "Your location is used to find and display nearby X facilities."
po alert.staticTexts.allElementsBoundByIndex[2] -> returns StaticText, {{32.0, 12.0}, {72.5, 16.0}}, label: 'Precise: On'
However, when I run the following (or runs during my test), it fails but as you can see from above, it does have a static text element at that position:
po alert.staticTexts.allElementsBoundByIndex[2].label -> No matches found for Element at index 2 from input {(
StaticText)} error: Execution was interrupted, reason: internal ObjC exception breakpoint(-8)
If I remain in the console and rerun the same call again, it then works though:
po alert.staticTexts.allElementsBoundByIndex[2].label -> "Precise: On"
Does anyone know what causes this error or a solution for how I can handle it in my test?
As this alert is "outside" your application and everything works when you debug/pause execution, I have to think this is something to do with synchronization.
Do you have anything waiting for the alert to fully display? I don't usually trust the UIInterruptionHandler to do more than auto-dismiss an alert not under test.
I'm using a page object model and typically wait for new pages in the initializer using something like _ = XCUIApplication().alerts.firstMatch.waitForExistence(timeout: 2.0)
That doesn't explain why your example of calling it twice in the console works, but timing is where I'd be looking. I feel that's the only time I ever see errors like this.
It could also be related to the map not being static or loading slowly and therefore changing the accessibility after XCUITest thinks it has finished loading and proceeds. Throwing in a nice long Thread.sleep would be the easiest way to debug that.
I try to update my project to Swift 3 and my app crash just after launching Xcode display the following message:
thread 1 EXC_BREAKPOINT (code=1, subcode=0x101959bfc)
here's the line where the problem happened
var list:[PFObject]! = []
(...)
let x = list[indexPath.row - 1]
let text:Int = (x["text"] as! NSArray)[0] as! Int //The line where the error is displayed
The console only said that:
(lldb)
I've removed all the breakpoints from the breakpoints navigator with no result, the issue happened both on device and on simulator. (I use Xcode 8.2)
Thanks a lot for your help
Take a look at the stack trace printed right before (lldb), and break down that statement in smaller pieces if is still hard to understand where the error actually is.
Considering the line the pointer is at, it could be that x["text"] is not an array, that the array is empty, or that the element is not an int.
In other words, those force unwrap could fail or the array could be empty.
Question
Is it possible to set breakpoints and debug code executed by Xcode / Interface Builder's Live Rendering? That is, I am using the IBDesignable and IBInspectable attributes to affect the view as it is rendered in an interface builder storyboard, and would like to be able to debug "IB crashed" errors. I don't recall this from the WWDC session, other than the section on prepareForInterfaceBuilder.
Context
I recently started seeing a number of errors reporting that IB crashed when attempting to render my code. I ultimately traced it to an otherwise innocent line of code in layoutSubviews in one of my views that was linked from the storyboard. The specific problem that IB - when rendering live views, called layoutSubviews before setting all the view's child view properties (which seems like a separate problem, in and of itself). This contrasted with the simulator and device, in which the views were set ahead of the first call to layoutSubviews. Long story short though, it involved diffing a bunch of files to find the offending change. Suffice to say, I could have found the problem quickly if it were possible to set breakpoints and trap the crash.
Has anyone figured out how to debug and/or generally trap these problems, or is that access not yet available?
I watched the WWDC video again (§411 #22:00 or so). You have to
edit a view in IB, and set its custom class to a class in your codebase
set breakpoints as desired in your custom class
select the view in IB, then select Editor -> "Debug Selected Views" (at bottom)
Oddly, in my tests today, it is creating my view via (initWith)Frame instead of (initWith)Coder. This causes subsequent constraint configuration logic to fail as the sub views have not been set (as they would had 'withCoder been called).
(tools note: Xcode 8.2, macOS Sierra)
I had a very different failure than yours: I had a crash in code that resulted in an IB error in Xcode:
error: IB Designables: Failed to update auto layout status: The agent crashed
To debug this on macOS Sierra, open Console.app, and open "User Reports" in the right sidebar. Find the "IBDesignablesCocoaTouch*" crashes, and select the applicable one. This gave me the code-level info I needed to diagnose it.
Crashed Thread: 0
Exception Type: EXC_BAD_INSTRUCTION (SIGILL)
Exception Codes: 0x0000000000000001, 0x0000000000000000
Exception Note: EXC_CORPSE_NOTIFY
Termination Signal: Illegal instruction: 4
Termination Reason: Namespace SIGNAL, Code 0x4
Terminating Process: exc handler [0]
Application Specific Information:
CoreSimulator 375.21 - Device: IBSimDeviceTypeiPad2x - Runtime: iOS 10.3 (14E8301) - DeviceType: IBSimDeviceTypeiPad2x
Thread 0 Crashed:
0 libswiftUIKit.dylib 0x000000021a2ddead _TToFE5UIKitCSo7UIImagecfT24imageLiteralResourceNameSS_S0_ + 205
1 libswiftUIKit.dylib 0x000000021a2ddcef _TFE5UIKitCSo7UIImageCfT24imageLiteralResourceNameSS_S0_ + 79
2 com.company.App 0x0000000216df74ad _TZFC10App21ControlsP33_F7118662B6663018C7DD0DEBB6FDD87511buttonImagefT8forStateOS_25State_CSo7UIImage + 109 (Controls.swift:121)
…
(In my case, Interface Builder was failing to load an image literal. I had to implement this workaround: xcode 6 IB_DESIGNABLE- not loading resources from bundle in Interface builder)
I'm trying to register my own class in a tableView so I could use the internal cell pool, but it crashes at the second I try to invoke this
this._cellIdentifier = new NSString(VignetteCell.vignetteIdentifier);
this._photosListTableView.RegisterClassForCellReuse(typeof(UITableViewCell), this._cellIdentifier);
I also tried with using "typeof(VignetteCell)" but it fails also. The weird thing is that it doesn't crash on the simulator.
This is what I get:
[ERROR] FATAL UNHANDLED EXCEPTION: MonoTouch.Foundation.MonoTouchException: Objective-C exception thrown. Name: NSInvalidArgumentException Reason: -[UITableView registerClass:forCellReuseIdentifier:]: unrecognized selector sent to instance 0x1cf3800
at MonoTouch.UIKit.UITableView.RegisterClassForCellReuse (IntPtr cellClass, MonoTouch.Foundation.NSString reuseIdentifier) [0x00027] in /Developer/MonoTouch/Source/monotouch/src/UIKit/UITableView.g.cs:743
at MonoTouch.UIKit.UITableView.RegisterClassForCellReuse (System.Type cellType, MonoTouch.Foundation.NSString reuseIdentifier) [0x00000] in /Developer/MonoTouch/Source/monotouch/src/UIKit/UITableView.cs:34
...
I just find nothing on Google mentioning it, as if I were the only guy on this planet who tried that!
Anyway, thanks for your help!
[Edit]
Solution: In fact, it just doesn't work on iOS 5. I tried it on my iPhone 5 and it just work fine.
What device/simulator are you running this on? Since this is a iOS 6 specific feature, it will need to be running on iOS 6 and running on iOS 5 will cause the following error message.
After upgrading to XCode4 (v. 4.2, 4D199) it seems every time my apps crash while debugging, the debugging points to main(), and the stack is unsymbolicated and useless.
This has been working fine for years, I have no idea what has gone wrong.
I'm using GDB. I also tried the LLDB as per this advice, and it didn't work either (similar, useless stack).
My breakpoints work, I get the full stack, and can inspect variables when my code hits those.
Steps to reproduce:
NB. this happens with my own project, but I'll use Apple's code here to remove that variable from the equation
Download the following sample from Apple: https://developer.apple.com/library/ios/#samplecode/UICatalog/Introduction/Intro.html#//apple_ref/doc/uid/DTS40007710
In the ImagesViewController class, add the following code to the viewDidLoad method (so it will crash – we want it to crash for this test):
// please note: this code is designed to crash! I want it to crash, to highlight my issue with XCode.
NSMutableArray* test = [NSMutableArray new];
[test insertObject:NULL atIndex:0];
Then run the app & hit the 'Images' row.
It crashes with a message like:
2011-12-23 14:07:02.788 UICatalog[13394:707] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '*** -[__NSArrayM insertObject:atIndex:]: object cannot be nil'
*** First throw call stack:
(0x37bbb8bf 0x316a11e5 0x37b1020f 0x699f 0x34fac7ff 0x34fb8c39 0x34fb8aa9 0x34fb898f 0x34fb815b 0x34fb7f53 0x34fac673 0x34fac349 0x66c1 0x35026565 0x3509ece7 0x31aec943 0x37b8fa63 0x37b8f6c9 0x37b8e29f 0x37b114dd 0x37b113a5 0x3768ffcd 0x34fa1743 0x2459 0x2418)
terminate called throwing an exception(gdb)
View in xcode:
Thanks to brigadir for pointing me to the solution!
It works well. Here's some screenshots for how to solve this for anyone finding my question:
Tap the plus button of the breakpoints tab
Then click Done