【问题标题】:If statement causes a crash in the iOS device but not in the Simulatorif 语句导致 iOS 设备崩溃,但模拟器中没有
【发布时间】:2014-02-27 21:19:53
【问题描述】:

因此,此代码运行正常,不会导致 iOS 模拟器出现任何崩溃。但是,在我的 iOS 设备(我的 iPhone)上,这会导致崩溃!谁能猜到为什么?日志在任何地方都没有帮助。

NSInteger loops = 1;
char character = '.';
if ([futureTr rangeOfString:[NSString stringWithFormat:@"%c",character]].location != NSNotFound) {
    NSArray *dotsArray = [futureTr componentsSeparatedByString:[NSString stringWithFormat:@"%c",character]];
    loops = [dotsArray count];
    if ([[dotsArray objectAtIndex:loops-1] isEqualToString:@""] || [dotsArray objectAtIndex:loops-1] == nil) {
        loops--;
    }
}

而且我知道正是这句话引起了麻烦,因为我把它注释掉了,应用程序就可以正常工作了!会发生什么?

【问题讨论】:

  • 完整的错误是什么?
  • 而NSArray 无法存储nil 值,因此无需检查。
  • 如果 [dotsArray count] 出于某种原因返回 0,这将崩溃,因为 loops 将是 0,loops-1 将是 -1,这永远不是有效的数组索引。
  • @rmaddy 是的,谢谢,但删除它并没有任何区别......不幸的是,我能找到的只是:“作业似乎已经崩溃:分段错误:11”并且它是SIGSEGV,BAD_ACCESS
  • 这对保留 futureTr 没有任何帮助。事实上,这会导致内存泄漏。试试NSString *keepTheFuture = [futureTr retain];。

标签: ios iphone objective-c if-statement ios-simulator


【解决方案1】:

根据 cmets 中的讨论,问题是 futureTr 在某个时候被释放,然后此代码尝试使用现在已释放的指针,从而导致“BAD_ACCESS”异常。

对这个变量进行适当的内存管理会缓解这个问题。

【讨论】:

    猜你喜欢
    • 2011-09-24
    • 1970-01-01
    • 2015-04-17
    • 2017-07-16
    • 2014-11-06
    • 2013-06-06
    • 2016-11-10
    • 2023-02-02
    • 1970-01-01
    相关资源
    最近更新 更多