如何修复“exc_bad_instruction (code=exc_i386_invop subcode=0x0)”

Posted

技术标签:

【中文标题】如何修复“exc_bad_instruction (code=exc_i386_invop subcode=0x0)”【英文标题】:How to fix "exc_bad_instruction (code=exc_i386_invop subcode=0x0)" 【发布时间】:2016-06-27 15:46:39 【问题描述】:

我想在我的应用程序中制作一个计时器,但我发现了这样一个问题:

我的代码在包含以下代码的 viewController 上运行得很好,但是在我关闭这个 viewController 几秒钟后,Xcode 会报告一个错误:exc_bad_instruction (code=exc_i386_invop subcode=0x0

如果我不使用dispatch_suspend(self.timer)dispatch_resume(self.timer),一切都会好的

否则,我可以看到 Xcode 处理 [viewController .cxx_destruct] 时会发生错误

那么,谁能告诉我如何解决它?

谢谢

这是我的代码,

- (void)timeButtonClick:(UIButton *)button
    if (self.isTiming == YES)
        self.isTiming = NO;
        self.executeTime = [self.timeButton currentTitle];
        dispatch_suspend(self.timer);
    else if (self.isTiming == NO)
        self.isTiming = YES;
        self.createDate = [NSDate date];
        dispatch_resume(self.timer);
    


- (dispatch_object_t)timer
    if (_timer == nil) 
        dispatch_queue_t timerQueue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);

        dispatch_source_t timer = dispatch_source_create(DISPATCH_SOURCE_TYPE_TIMER, 0, 0, timerQueue);

        dispatch_source_set_timer(timer, DISPATCH_TIME_NOW, 1 * NSEC_PER_SEC, 0 * NSEC_PER_SEC);

        __weak typeof (self)weakSelf = self;
        dispatch_source_set_event_handler(timer, ^

            NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
            dateFormatter.dateFormat = @"HH:mm:ss";

            NSCalendar *calendar = [NSCalendar calendarWithIdentifier:NSCalendarIdentifierGregorian];
            NSCalendarUnit unit = NSCalendarUnitHour | NSCalendarUnitMinute | NSCalendarUnitSecond;

            NSDate *currentDate = [NSDate date];
            NSDateComponents *currentCmps = [calendar components:unit fromDate:weakSelf.createDate toDate:currentDate options:NSCalendarWrapComponents];

            NSDate *executeTime = [dateFormatter dateFromString:weakSelf.executeTime];
            NSDateComponents *executeCmps = [calendar components:unit fromDate:executeTime];

            NSString *newExecuteTime = [NSString stringWithFormat:@"%02ld:%02ld:%02ld", executeCmps.hour + currentCmps.hour, executeCmps.minute + currentCmps.minute, executeCmps.second + currentCmps.second];

            dispatch_async(dispatch_get_main_queue(), ^

                [weakSelf.timeButton setTitle:newExecuteTime forState:UIControlStateNormal];

            );
        );

      _timer = timer;
    
  return _timer;

【问题讨论】:

【参考方案1】:

将您的计时器移动到另一个地方,例如 AppDelegate,您会收到此错误,因为您正在将计时器分派到另一个线程,然后当 VC 被关闭时,计时器想要将标题设置为 timeButton,当 VC 被销毁时,它已经被销毁了被解雇了。

你也可以试着做一个强弱的自己,但我真的建议你把计时器代码移到另一个类。

【讨论】:

以上是关于如何修复“exc_bad_instruction (code=exc_i386_invop subcode=0x0)”的主要内容,如果未能解决你的问题,请参考以下文章

在 XCode 6.1.1 中调试 EXC_BAD_INSTRUCTION 崩溃

Swift 解析 Facebook Graph API 对象时出错:EXC_BAD_INSTRUCTION(code=EXC_I386_INVOP)

Swift 错误 - 线程 1:EXC_BAD_INSTRUCTION

Swift-音频实现崩溃:“EXC_BAD_INSTRUCTION”

EXC_BAD_INSTRUCTION 运行时错误

通过按钮替换 ViewController,线程 1:EXC_BAD_INSTRUCTION