legolas
legolas

Reputation: 745

what does Error "Thread 1:EXC_BAD_INSTRUCTION (code=EXC_I386_INVOP, subcode=0x0)" mean?

I got this error:

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

How can I solve this? The code works normally, but in the calculator when I click the only equal button, it gives that error.

@IBAction func equals(sender: AnyObject) {

    secondNumber = Screen.text!.toInt()!  // here it shows an error which is "Thread 1:EXC_BAD_INSTRUCTION (code=EXC_I386_INVOP, subcode=0x0)"

    if operation == "+"{
        result = firstNumber + secondNumber
    }
    else if operation == "-" {
        result = firstNumber - secondNumber
    }
    else if operation == "x" {
        result = firstNumber * secondNumber
    }
    else {
        result = firstNumber / secondNumber
    }
    Screen.text = "\(result)"
}

Upvotes: 72

Views: 168453

Answers (14)

yoAlex5
yoAlex5

Reputation: 34235

EXC_BAD_INSTRUCTION (code=EXC_I386_INVOP, subcode=0x0)

You are able to run into this error when you use Core Data Concurrency Debugging on launch Arguments. App throws this error when you access a managed object on the wrong queue[About]

-com.apple.CoreData.ConcurrencyDebug 1

Upvotes: 0

iOS_Tejas
iOS_Tejas

Reputation: 269

  • In my case, I am reusing the tableview_cell class in 2 different controller classes,

  • and I added a new outlet to one of the cell but it's not available for the other controller cell class, because of this it showed a connection in class, not able to find it's broken for other class, so I missed the connection for the other class.

  • need to add a connection for that outlet.

Upvotes: 0

mykolaj
mykolaj

Reputation: 974

In my case it was because I was trying to start (NS)Operation by directly using its start() function. But instead what I needed to do is to pass it to OperationQueue. Something like this var queue = OperationQueue(); queue.addOperation(myOperation)

Upvotes: 0

Vova Denys
Vova Denys

Reputation: 32

try to clear workspace.

rm -rf ' ~/Library/Application\ Support/"your programm name" '

Upvotes: -2

gnasher729
gnasher729

Reputation: 52538

This line

secondNumber = Screen.text!.toInt()!

means: Get the Screen object, get the text property and please crash if it doesn't exist, then get the text converted to an integer, and please crash if it doesn't exist.

That's what the ! means: "I am sure this thing exists, so please crash if it doesn't". And crash is what it did.

Upvotes: 137

Andres Canella
Andres Canella

Reputation: 3716

I got this on WatchOS Sim. The issue persisted even after:

  • Deleting the app
  • Restarting Xcode
  • Deleting Derived Data

...and was finally fixed by "Erase all Content and Settings" in the simulator.

Upvotes: 0

hrmncd
hrmncd

Reputation: 1045

In my case it happened when calling a function by passing a parameter of a Core Data managed object's property. At the time of calling the object was no longer existed, and that caused this error.

I have solved the issue by checking if the managed object exists or not before calling the function.

Upvotes: 4

Helge Becker
Helge Becker

Reputation: 3243

Your secondNumber seems to be an ivar, so you have to use a local var to unwrap the optional. And careful. You don't test secondNumber for 0, which can lead into a division by zero. Technically you need another case to handle an impossible operation. For instance checkin if the number is 0 and do nothing in that case would at least not crash.

@IBAction func equals(sender: AnyObject) {

    guard let number = Screen.text?.toInt(), number > 0 else {
        return
    }

    secondNumber = number

    if operation == "+"{
        result = firstNumber + secondNumber
    }
    else if operation == "-" {
        result = firstNumber - secondNumber
    }
    else if operation == "x" {
        result = firstNumber * secondNumber
    }
    else {
        result = firstNumber / secondNumber
    }
    Screen.text = "\(result)"
}

Upvotes: 2

Szilveszter Zsigmond
Szilveszter Zsigmond

Reputation: 96

If someone else have got a similar error message, you probably built the app with optimisations(production) flag on and that's why the error message is not that communicative. Another possibility that you have got the error under development (from i386 I know you were using simulator). If that is the case, change the build environment to development and try to reproduce the situation to get a more specific error message.

Upvotes: 0

J. Doe
J. Doe

Reputation: 13043

I got this error while I tried to write to a variable at the same time from different threads. Creating a private queue and making sure one thread at a time can write to that variabele at the same time. It was a dictionary in my case.

Upvotes: 4

pankaj nigam
pankaj nigam

Reputation: 401

mine was DispatchQueue.main.sync inside the closer I made it DispatchQueue.main.async and it worked.

Upvotes: 6

Kiwi
Kiwi

Reputation: 145

In my case this was caused by an integer overflow. I had a UInt16, and was doubling the value to put into an Int. The faulty code was

let result = Int(myUInt16 * 2)

However, this multiplies as a UInt16, then converts to Int. So if myUInt16 contains a value over 32767 then an overflow occurs.

All was well once I corrected the code to

let result = Int(myUint16) * 2

Upvotes: 3

alicanbatur
alicanbatur

Reputation: 2200

Mine was about

dispatch_group_leave(group)

was inside if closure in block. I just moved it out of closure.

Upvotes: 10

Ashraf Tawfeeq
Ashraf Tawfeeq

Reputation: 3029

Generally, EXC_BAD_INSTRUCTION means that there was an assertion failure in your code. A wild guess, your Screen.text is not an integer. Double check its type.

Upvotes: 14

Related Questions