SwiftUI :Thread 1: EXC_BAD_INSTRUCTION (code=EXC_I386_INVOP, subcode=0x0) - swift

I am new in SwiftUI and I am currently developing my first big application.The program runs successfully in the simulator however the simulator screen is all white and I get the error:
Thread 1: EXC_BAD_INSTRUCTION (code=EXC_I386_INVOP, subcode=0x0)
in the AppDelegate(to which I have made no changes)
the app already consists of multiple files , however I can not find any problems in my code however many times I check it. What type of error should I be looking for?

In the context of Swift code,
EXC_BAD_INSTRUCTION
usually means you’ve hit a compiler trap, that is, an undefined instruction inserted into the code by the compiler because of a bug detected at runtime. The most common cause of these are:
failure to unwrap an optional —
This can be a forced unwrap (!) or an implicit unwrap (accessing an implicitly unwrapped optional that’s nil).
array out of bounds
a failed forced cast (as!), either because the value was a nil optional or because the value was of the wrong type
You can debug this issue by creating a exception breakpoint. As the name suggests, this stops the code execution before the execution of the line that throwed this exception.
To Create a exception breakpoint in Xcode, Go to BreakPoint navigator -> Click the + icon at the bottom left corner -> Select Exception Breakpoint.
More about breakpoints check this link

Related

Why am I getting the error "EXC_BAD_ACCESS"

I am trying to debug my Autolayout using the suggestions of this website, to highlight views that are causing constraint issues. However when I try to change the color of the suspect view with the command
expr ((UIView *)0x7f9ea3d43410).backgroundColor = [UIColor redColor]
I get the Error
error: Execution was interrupted, reason: EXC_BAD_ACCESS (code=1, address=0xcd4200020).
The process has been returned to the state before expression evaluation.
I have also gotten the error:
error: Execution was interrupted, reason: Attempted to dereference an invalid ObjC Object or send it an unrecognized selector.
The process has been returned to the state before expression evaluation.
I am not sure why I am getting this error as I have made sure to replace the example hex code with my own, what does this error even mean?
I am coding in swift, the website I reference uses objective-c, that may be my problem. what would the swift code be to do the same thing? I have tried:
expr ((UIView *)0x7f9ea3d43410).backgroundColor = UIColor.redColor
I think the short answer is "that link is from 2015 and the recommended technique uses hidden internal symbols that are no longer supported."
Specifically, the symbol is _autoLayoutTrace. That is a private symbol which apparently has been dropped.

How do I get more information about this exception?

I get the following error in XCode...
I've added a "Swift Error" breakpoint and left the "Type" box empty. I thought this might show some further info, but it doesn't appear to have any effect.
How can I find out where this error is arising from?
EXC_BAD_INSTRUCTION means that you have an invalid assertion (usually a force-unwrapped nil, though a bad cast could also be the culprit, here). Make sure that tourDto is a populated var, and that it can be cast to whatever toJSONString() returns (I'm not familiar with that method, and it could also be the culprit).

Break on any occurrence of "fatal error: unexpectedly found nil while unwrapping an Optional value"

I have a huge code base and I want to break on the error "fatal error: unexpectedly found nil while unwrapping an Optional value"
What is the best approach to do that?
I tried adding both: (In the breakpoints sub-window)
"Add Swift Error Breakpoint"
"Add Exception Breakpoint"
But that didn't do it. Does that mean that the nil was in some framework code and not my code?
Unwrapping optionals gets translated by the compiler into calls to _getOptionalValue(), which calls _preconditionFailure() in case of anil value, which gets translated into a call to _fatalErrorMessage().
So you need to set breakpoints for _fatalErrorMessage. The trick here is that you need to set the breakpoints for the mangled name of the function, which is _TTSf4s_s_d_d___TFSs18_fatalErrorMessageFTVSs12StaticStringS_S_Su_T_.
If you add a symbolic breakpoint for this, the breakpoint will be hit everytime a fatal error occurs. This means you'll catch all nil unwraps, however you'll also catch some other pieces of code that trigger fatal errors. You should be able to set some conditions on the arguments of the function so that your breakpoint is hit only when the unexpectedly found nil while unwrapping an Optional value is passed, however I personally didn't played with this feature of lldb so I'm not sure how you need to set this condition in the breakpoint editor.
Update. In Swift 3 (Xcode 8 beta 6), the breakpoint should be set for _TTSfq4n_n_d_d_n___TFs18_fatalErrorMessageFTVs12StaticStringS_S_Su5flagsVs6UInt32_Os5Never
Here's how you can find out for your own the mangled function name for any Swift version, in order to set a breakpoint to:
Find you build app location (can be easily found in Xcode Navigator -> Products -> Your App Name.app)
List all your global symbols for the libswiftCore.dylib embeded within your app bundle, grep by fatalError. So for example if your build app is at /Users/your.username/Library/Developer/Xcode/DerivedData/YourApp-randomchars/Build/Products/Debug-iphonesimulator/YourApp.app/, you should run
nm -g /Users/your.username/Library/Developer/Xcode/DerivedData/YourApp-randomchars/Build/Products/Debug-iphonesimulator/YourApp.app//Frameworks/libswiftCore.dylib | grep fatalError
Check the output and grab the mangled method names. There could be more than one symbol, if the function is overloaded. A sample output looks like this:
0000000000032f50 T __TFs18_fatalErrorMessageFTVs12StaticStringS_S_Su5flagsVs6UInt32_Os5Never
00000000001663b0 T __TTSfq4n_n_d_d_n___TFs18_fatalErrorMessageFTVs12StaticStringS_S_Su5flagsVs6UInt32_Os5Never
Make sure you remove the leading underscore before adding the breakpoint, that one comes from the C name mangling (so for the above output you should remain with only one leading underscore for the two symbols).
Add the breakpoints and force unwrap a nil value, to make sure they work.
Happy nil-unwrapping hunting :)

fatal error: unexpectedly found nil while unwrapping an Optional value While unwrapping SKSpriteNode

I am getting the following error at the mentioned line of code
Even though the debugger shows that bg has been assigned a value, that it fetches from .sks file. (GameScene.sks file contains an object named as bg)
Any help would be highly appreciated.
-> -> EDIT
Whilst no thanks to the community here thats full of people who'd just try to be uncanningly witty and gods of humor, i managed to figure it out myself. So for a future reference if someone else falls in the same situation, i am sharing the code that was the reason for the problematic error.
At this point in time the debugger will show a value for bg that was previously there on the stack (garbage in the memory).
It will never write something to bg because the program crashes while evaluating the value for the variable so you are just seeing garbage.
The error you can see clearly means there is no node named bg.

Diagnosing EXC_BAD_INSTRUCTION in Swift standard library

My Swift application running in iOS simulator is being stopped in debugger with runtime error EXC_BAD_INSTRUCTION (code=EXC_I386_INVOP, sub code=0x0).
According to the WWDC 2014 Session 409 this is typically due to assertion failure.
In the current development Beta version of Xcode 6, the debugger’s stack trace and the above error does not provide enough information to see what the issue is. How do I find out where the problem is?
It appears that the most common source of this error (at the time of this writing: Xcode 6 Beta 1) is that some implicitly unwrapped optional property or variable is nil.
For convenience, most Objective-C API are bridged to Swift with implicitly unwrapped optionals. They are denoted by exclamation mark behind the type declaration: AnyObject[]!
If the debugger stops in your code, double-check that line and look for implicitly unwrapped optionals that could be nil there.
Sometimes the debugger stops with that runtime error deep in Swift system library. This happens for example when you pass a closure to collection methods like filter, map, reduce et al. The runtime error then occurs at the call site of those library functions, but the definition might be in different parts of your code, where you defined the function/closure. Look there for implicitly unwrapped optionals that might be nil at runtime.
To guard agains these kinds of error be aware that even though Swift compiler will not force you to handle the potential nil values returned from Cocoa, you should use optional binding, optional chaining
or optional downcasting wherever the return value from Objective-C land might be nil.
Let’s hope that future versions of Swift compiler will start emitting more helpful diagnostic messages and errors for this common type of problem!
I have found (after many hours) that this error can appear on the wrong line.
For example
As you can see the app is crashing where I am checking for nil, but then 'continuing' through because it prints the statements. It then goes 'backwards' and crashes.
I've come to the conclusion that there is a source-mapping bug in XCode (7) where a nil variable is un-wrapped. In this case, I had a variable (much farther down in my code) that was nil and being unwrapped.
The problem of course is that the compiler didn't flag the actual variable that was nil, it flagged something else entirely.
So if you run into this nasty bug, go through all possible variables that can be nil and check them for unwrapping. You are likely unwrapping a nil, its just not the one the compiler says.
As mentioned in the comment there is compiler optimization. Here is a link to fix the issue (and find the route cause of the crash)
xcode 6.1 how to disable optimization (Swift)
I had the same issue as Palimondo. Fortunately, it was simply a matter of making sure that I had initialized the item ahead of time. In my code, I was calling a function to place images in UIImageViews and passing in an element from an array. I had not yet loaded my array with the UIImageViews, and therefore when the code would run it would say that I was passing in a nonexistent element of the array. Once I made sure to load up my array at the start of the program the error went away.