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.
Related
I have a utility method for logging various non-fatal errors in my project. In this method I also send the error to Crashlytics by its record() method, but when looking at Crashlytics, all errors are grouped together into one instance, beacause the last info in the stacktrace is line x of my UtilityClass (where Crashlytics.crashlytics().record(error: error) is called).
Can I remove from the stacktrace the reference about the last method before calling Crashlytics.crashlytics().record(error: error)? And how to do it?
There are other ways?
I'm looking for a way to tell Crashlytics that all my non-fatal events are not coming from the same line of code, but from the line that calls that line, so that all the events are not grouped togheter.
In general, the crashes will be grouped together based on the highlighted blamed frame and not necessarily by the top frame in the stack trace. This means that crashes with different stack traces could end up in the same issue if the same frame is blamed on both.
This is what I mean by blamed frame:
If the latest frame is related to your utility method and that is the one being framed, expanding on what you mentioned in the comment about using the ExceptionModel, I came up with this workaround:
var exMod = ExceptionModel.init(name: NSURLErrorDomain, reason: "some reason")
var stack: [StackFrame] = [];
// The current stack trace is obtained after removing the first element
for frame in Thread.callStackSymbols.dropFirst(){
// The frame is a String, the address value is obtained and appended to the stack array.
let frameArray = frame.description.split(separator: " ");
// The address is a String and is converted to a UInt value
let address = UInt(frameArray[2].dropFirst(2), radix: 16) ?? 0
stack.append(StackFrame(address: address));
}
// The stack trace is added to the ExceptionModel
exMod.stackTrace = stack;
Crashlytics.crashlytics().record(exceptionModel: exMod)
This is the result of running this snippet without “.dropFirst()”:
And this is the result dropping the first element:
As you can see, the first element was removed as expected.
My suggestion would be to first try causing different crashes on different parts of your app and see if the same frame is getting blamed. I.e: Checking if those crashes keep getting grouped.
Then check if the above snippet works for your use case.
I'm currently facing a problem with some Swift source files when a crash occurs. Indeed, on Crashlytics I have a weird info about the line and the reason of the crash. It tells me the source has crashed at the line 0 and it gives me a SIGTRAP error. I read that this error occurs when a Thread hits a BreakPoint. But the problem is that this error occurs while I'm not debugging (application test from TestFlight).
Here is an example when Crashlytics tells me there's a SIGTRAP Error at line 0 :
// Method that crashs
private func extractSubDataFrom(writeBuffer: inout Data, chunkSize: Int) -> Data? {
guard chunkSize > 0 else { // Prevent from having a 0 division
return nil
}
// Get nb of chunks to write (then the number of bytes from it)
let nbOfChunksToWrite: Int = Int(floor(Double(writeBuffer.count) / Double(chunkSize)))
let dataCountToWrite = max(0, nbOfChunksToWrite * chunkSize)
guard dataCountToWrite > 0 else {
return nil // Not enough data to write for now
}
// Extract data
let subData = writeBuffer.extractSubDataWith(range: 0..<dataCountToWrite)
return subData
}
Another Swift file to explain what happens at the line "writeBuffer.extractSubDataWith(range: 0..
public extension Data {
//MARK: - Public
public mutating func extractSubDataWith(range: Range) -> Data? {
guard range.lowerBound >= 0 && range.upperBound <= self.count else {
return nil
}
// Get a copy of data and remove them from self
let subData = self.subdata(in: range)
self.removeSubrange(range)
return subData
}
}
Could you tell me what I'm doing wrong ? Or what can occurs this weird SIGTRAP error ?
Thank you
Crashing with a line of zero is indeed weird. But, common in Swift code.
The Swift compiler can do code generation on your behalf. This can happen quite a bit with generic functions, but may also happen for other reasons. When the compiler generates code, it also produces debug information for the code it generates. This debug information typically references the file that caused the code to be generated. But, the compiler tags it all with a line of 0 to distinguish it from code that was actually written by the developer.
These generic functions also do not have to be written by you - I've seen this happen with standard library functions too.
(Aside: I believe that the DWARF standard can, in fact, describe this situation more precisely. But, unfortunately Apple doesn't seem to use it in that way.)
Apple verified this line zero behavior via a Radar I filed about it a number of years ago. You can also poke around in your app's own debug data (via, for example dwarfdump) if you want to confirm.
One reason you might want to try to do this, is if you really don't trust that Crashlytics is labelling the lines correctly. There's a lot of stuff between their UI and the raw crash data. It is conceivable something's gone wrong. The only way you can confirm this is to grab the crashing address + binary, and do the lookup yourself. If dwarfdump tells you this happened at line zero, then that confirms this is just an artifact of compile-time code generation.
However, I would tend to believe there's nothing wrong with the Crashlytics UI. I just wanted to point it out as a possibility.
As for SIGTRAP - there's nothing weird about that at all. This is just an indication that the code being run has decided to terminate the process. This is different, for example, from a SIGBUS, where the OS does the terminating. This could be caused by Swift integer and/or range bounds checking. Your code does have some of that kind of thing in both places. And, since that would be so performance-critical - would be a prime candidate for inline code generation.
Update
It now seems like, at least in some situations, the compiler also now uses a file name of <compiler-generated>. I'm sure they did this to make this case clearer. So, it could be that with more recent versions of Swift, you'll instead see <compiler-generated>:0. This might not help tracking down a crash, but will least make things more obvious.
I've been trying to figure out how this innocent line could crash the app. The same code works on 64-bit simulators (iPhone 5s or later) but would crash on 32-bit simulators whenever new_or_used (an optional String in an NSManagedObject) is accessed. The stack trace does not seem helpful.
Any insights appreciated. Thanks!
According to your image seems the compiler try to use an already deallocated String.
To avoid your crash, try to correct your code as :
if let newOrUsed = itemPriceGuide.new_or_used as? String {
// do your stuff
}
This happened again just recently, and it was due to the variable starting with "new". NSPersistentStoreCoordinator throws EXC_BAD_ACCESS when deallocating an NSManagedObject after reading a specific NSManagedProperty
I'm on macOS Sierra, Xcode 8, and get crashes whenever I try to Simulate Background Fetch on an actual iOS 10 device. This does NOT occur when using the simulator. This occurs in all projects, including those freshly created.
libsystem_kernel.dylib`mach_msg_trap:
0x18cfec164 <+0>: movn x16, #0x1e
0x18cfec168 <+4>: svc #0x80
-> 0x18cfec16c <+8>: ret (Thread 1: signal SIGSTOP)
I don't have any other iOS devices to test with; is anyone else experiencing this?
I'm running Xcode 8.1 with a device - it's not crashing but it's hitting a breakpoint that you can't find/edit/remove.
I go to Debug > Continue and it carries on as expected.
You aren't alone - I'm encountering this as well. Really annoying. I've just filed a bug.
As pointed out by others. it is not a crash, but an auto breakpoint triggered by system. As explained here by eskimo:
So, why does it stop in mach_msg_trap? The goal is to give you a
chance to set a breakpoint in your background fetch handling code, so
Xcode forces your app to stop, just like it would if you hit the pause
button (Debug > Pause). It stops in mach_msg_trap because that’s
where your main thread is sitting when your app is suspended in the
background. In short, when you do a Debug > Simulate Background Fetch
and stop in mach_msg_trap:
That’s expected behaviour
It’s not a sign of any problem
You can safely continue execution via Debug > Continue
Personally, I see it more like a bug than a feature.
It's not crashing, something is throwing a signal to pause execution of the app. Without knowing how the simulated fetch happens I can only guess why - it might be part of the simulation or a side effect of the way the app handles it. Xcode used to support permanently disabling breaks on signals (SIGPIPE was always my bane), but that hasn't worked for several versions now.
TARGETS -> Gapabilities -> Background Modes -> ON
and check [Audio, AirPlay, and Picture in Picture]
It worked for me.
I just had this issue, it had something to do with pulling data from my backend and a casting type.
If you aren't familiar with firebase just look at the last two lines of this. If you are, here is how I fixed it. My data coming in looked something like this
"Node" : {
"SubNode" : {
"-KoB8OMIO0PLiTs8fUkJ" : {
"ImageName" : "DSC05833-2.jpg",
"price" : 100,
},
"-KoB8Rh9PtSMaMUlaD91" : {
"ImageName" : "DSC05780-2.jpg",
"price" : 0,
},
And my code to pull.
ref.child("Node").child("SubNode").child(uniqueidID).observeSingleEvent(of: DataEventType.value, with: { (snapshot) in
if snapshot.childrenCount > 0
{
let jsonPhoto = snapshot.value as? [String: AnyObject]
let mageName = jsonPhoto?["ImageName"] as! String
photoObj.imagePrice = jsonPhoto?["price"] as! Double //error, Swift didn't catch, just froze my app
}
})
it worked if value was 100 -> Double, and then for some reason firebase was failing when I casted 0 -> Double
So I fixed how I casted it
I originallyy had this:
photoObj.imagePrice = jsonPhoto?["price"] as! Double // caused error
to..
photoObj.imagePrice = jsonPhoto?["price"] as? Double ?? 0.00 //fixed error
I've been playing around with Swift code to get accustomed to it, but when I run the code snippet in main.swift file (not a playground), the error Thread 1: EXC_BAD_INSTRUCTION (code=EXC_I386_INVOP, subcode=0x0) was shown on the screen.
The error points to the following line:
// loop
let scores:Int[] = [52, 53, 12, 53]
for score in scores {
println("The score is \(score)")
} // <- the error points to this line
However, I don't know why the code above is subject to the error. Is there anything wrong with the snippet above?
Even if I rewrite the above code to the following (add the explicit type to the variable within loop):
// loop
let scores:Int[] = [52, 53, 12, 53]
for score:Int in scores {
println("The score is \(score)")
}
, the error is still there.
So how can I cope with the issue here? I use Xcode 6 beta on OS X Yosemite dev preview.
EXC_BAD_INSTRUCTION means that there was an Assertion that failed in the code. Likely this is an assertion within the swift standard library, for safety reasons.
You should be able to look at the call stack when the program stops from the runtime error to investigate where the problem is and what is causing it.