com.apple.main-thread EXC_BAD_ACCESS KERN_INVALID_ADDRESS(来自iOS 8.x的NSTimer)

时间:2017-08-09 05:47:21

标签: ios objective-c nstimer nsnotificationcenter crashlytics

遇到崩溃的崩溃。

Crashed: com.apple.main-thread

0  libobjc.A.dylib                0x197e37bdc objc_msgSend + 28
1  Foundation                     0x1881192c8 -[NSCFTimer initWithFireDate:interval:target:selector:userInfo:repeats:] + 140
2  Foundation                     0x188123d3c +[NSTimer(NSTimer) scheduledTimerWithTimeInterval:target:selector:userInfo:repeats:] + 140
3  RV PetSafety                   0x1000fd2a0 -[HomeViewController UIApplicationDidBecomeActive:] (HomeViewController.m:179)
4  CoreFoundation                 0x1872dc1e0 __CFNOTIFICATIONCENTER_IS_CALLING_OUT_TO_AN_OBSERVER__ + 20
5  CoreFoundation                 0x18721b370 _CFXNotificationPost + 2060
6  Foundation                     0x188116cc0 -[NSNotificationCenter postNotificationName:object:userInfo:] + 72
7  UIKit                          0x18bb664c0 -[UIApplication _stopDeactivatingForReason:] + 464
8  UIKit                          0x18bd77818 -[UIApplication _handleNonLaunchSpecificActions:forScene:withTransitionContext:] + 2664
9  FrontBoardServices             0x18f56d640 __31-[FBSSerialQueue performAsync:]_block_invoke + 28
10 CoreFoundation                 0x1872ee124 __CFRUNLOOP_IS_CALLING_OUT_TO_A_BLOCK__ + 20
11 CoreFoundation                 0x1872ed22c __CFRunLoopDoBlocks + 312
12 CoreFoundation                 0x1872eb850 __CFRunLoopRun + 1756
13 CoreFoundation                 0x1872191f4 CFRunLoopRunSpecific + 396
14 GraphicsServices               0x1903835a4 GSEventRunModal + 168
15 UIKit                          0x18bb4a784 UIApplicationMain + 1488
16 RV PetSafety                   0x1001161e8 main (main.m:14)
17 libdyld.dylib                  0x198492a08 start + 4

我的代码:

- (void)UIApplicationDidBecomeActive:(NSNotification *)notification {
    if([updateTimer isValid]) {
        [updateTimer invalidate];
    }
    updateTimer = [NSTimer scheduledTimerWithTimeInterval:refreshInterval
                                                   target:self
                                                 selector:@selector(refreshData)
                                                 userInfo:nil
                                                  repeats:YES];
}

每当我的应用变为有效时,我想更新NSTimer时间。我需要调用API一段时间。我在这个方法上遇到了崩溃。在具有API调用的上述选择器方法refreshData上。在NSNotificationCenter

初始化viewWillAppear
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(UIApplicationDidBecomeActive:)
                                             name:UIApplicationDidBecomeActiveNotification object:nil];

该行的Crashlytics标题HomeViewController.m line 179 -[HomeViewController UIApplicationDidBecomeActive:]

updateTimer = [NSTimer scheduledTimerWithTimeInterval:refreshInterval
                                                   target:self
                                                 selector:@selector(refreshData)
                                                 userInfo:nil
                                                  repeats:YES];

我在这个项目中没有看到任何[[NSNotificationCenter defaultCenter] removeObserver:self name:UIApplicationDidBecomeActiveNotification object:nil];。刚才我添加了但是,我想确认是因为多个addObserverNSTimer初始化而导致崩溃?

更新16/08/2017:

在几次iOS版本8.x

中出现此错误
  

iOS版:8.4.1(12H321)

     

型号:iPhone 5c(型号A1456,A1532)

2017年8月17日更新:

第一次崩溃发生在3个月以前,第二次发生超过2周。我无法在Xcode-organizer崩溃标签中获取崩溃详情。我在Crashlytics中看到了其他一些崩溃,而不是Xcode-organizer。与Crashlytics

相比,我无法理解Xcode-organizer崩溃详情

1 个答案:

答案 0 :(得分:0)

你不能将观察者代码放在viewWillAppear上,因为代码会调用毁伤时间:

[[NSNotificationCenter defaultCenter] addObserver:self
                                         selector:@selector(UIApplicationDidBecomeActive:)
                                             name:UIApplicationDidBecomeActiveNotification
                                           object:nil];