EXC_BAD_INSTRUCTION on dispatch_sync()

The following is my code to show text in textview. But the error, EXC_BAD_INSTRUCTION, happens while executing it.

How does this error happen and how could I fix it?

dispatch_sync(dispatch_get_main_queue(), ^{
        [[[Memo1 textStorage] mutableString]appendString:[NSString stringWithFormat:@"%@", str]];
        [Memo1 scrollRangeToVisible:NSMakeRange(Memo1.string.length, 0)];//auto scroll down
        });

Should it be Memo1.string.length - 1 ?

EXC_BAD_INSTRUCTION has two common causes:

  • The code called a bad function pointer.

  • The code hit a trap.

It’s hard to say much without more details. Please post a full crash report. See Posting a Crash Report for advice on how to do that.

Share and Enjoy

Quinn “The Eskimo!” @ Developer Technical Support @ Apple
let myEmail = "eskimo" + "1" + "@" + "apple.com"

This is the whole code of that function to show the str in text view. Memo1 is the textview outlet.

ERROR happens calling this function.

-(void)showMeg:(NSString *)str
{
  dispatch_sync(dispatch_get_main_queue(), ^{
          [[[Memo1 textStorage] mutableString]appendString:[NSString 
  stringWithFormat:@"%@", str]];
          [Memo1 scrollRangeToVisible:NSMakeRange(Memo1.string.length, 0)];//auto scroll down
          });
}

Someone suggested me change dispatch_sync to dispatch_async or close the dispatch_sync with a dispatch_async like following.

Then the error, EXC_BAD..., disappeared but I found the order of the strings showed on textview is wrong few times, not often.

For example, I'd like to show 0x01, 0x02, 0x03. But it shows 0x01, 0x03, 0x02.

    dispatch_async(dispatch_get_global_queue(0, 0), ^{

        dispatch_sync(dispatch_get_main_queue(), ^{

            [[[Memo1 textStorage] mutableString]appendString:[NSString stringWithFormat:@"%@", str]];

            [Memo1 scrollRangeToVisible:NSMakeRange(Memo1.string.length, 0)];//auto scroll down

        });

    });

Someone suggested me change dispatch_sync to dispatch_async

That’d be my general inclination too. Dispatching synchronously from a secondary thread to the main thread is a common cause of deadlocks. I’m not sure how it triggered the EXC_BAD_INSTRUCTION crash though. For that I really do need to see a full crash report.

Share and Enjoy

Quinn “The Eskimo!” @ Developer Technical Support @ Apple
let myEmail = "eskimo" + "1" + "@" + "apple.com"

EXC_BAD_INSTRUCTION on dispatch_sync()
 
 
Q