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

时间:2016-06-27 15:46:39

标签: ios objective-c xcode

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

我的代码在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 个答案:

答案 0 :(得分:0)

将你的计时器移动到另一个地方,比如AppDelegate,你得到这个错误,因为你正在将计时器调度到另一个线程,然后当VC被解除时,计时器想要将标题设置为timeButton,当VC是驳回。

您也可以尝试强弱自己,但我真的建议您将计时器代码移到另一个类。