【问题标题】:Unexplained crash with NSTimeIntervalNSTimeInterval 无法解释的崩溃
【发布时间】:2011-11-02 13:55:25
【问题描述】:

在没有太多调试线索的情况下,我似乎无法找出导致我的应用程序严重崩溃的代码更改错误。

这是原来的方法

+ (NSArray *)currentReservations {
    NSTimeInterval interval = [[NSDate date] timeIntervalSince1970];
    double futureTimeframe = interval + SecondsIn24Hours;
    NSArray *reservations = [Reservation findWithSql:@"select * from Reservation where timestamp < ? and timestamp > ?" withParameters:[NSArray arrayWithObjects:[NSNumber numberWithDouble:ceil(futureTimeframe)], [NSNumber numberWithDouble:floor(interval)], nil]];
    return reservations;
}

该方法设置了一些变量,因此我可以查询数据库以查找具有从现在到未来 24 小时之间时间戳的所有记录。我需要更改查询从现在到明天(次日午夜)之间时间戳的所有记录的方法,因此我根据this other stackoverflow question 将代码更新为此

+ (NSArray *)currentReservations {
    NSDate *today = [NSDate date];
    NSCalendar *gregorian = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
    NSDateComponents *components = [[NSDateComponents alloc] init];
    [components setDay:1];  // tomorrow
    NSDate *tomorrow = [gregorian dateByAddingComponents:components toDate:today options:0];
//    [components release];  // dont think we need this release, but it is in the example here: https://stackoverflow.com/questions/181459/is-there-a-better-way-to-find-midnight-tomorrow
    NSUInteger unitFlags = NSYearCalendarUnit | NSMonthCalendarUnit | NSDayCalendarUnit;
    components = [gregorian components:unitFlags fromDate:tomorrow];
    [components setHour:0];
    [components setMinute:0];
    NSDate *tomorrowMidnight = [gregorian dateFromComponents:components];
    [components release], components=nil;
    [gregorian release], gregorian=nil;


    NSTimeInterval interval = [today timeIntervalSince1970];
    NSTimeInterval tomorrowInterval = [tomorrowMidnight timeIntervalSince1970];

    NSArray *reservations = [Reservation findWithSql:@"select * from Reservation where timestamp < ? and timestamp > ?" withParameters:[NSArray arrayWithObjects:[NSNumber numberWithDouble:tomorrowInterval], [NSNumber numberWithDouble:floor(interval)], nil]];
    return reservations;
}

但是,当这两行:

    NSTimeInterval interval = [today timeIntervalSince1970];
    NSTimeInterval tomorrowInterval = [tomorrowMidnight timeIntervalSince1970];

包括应用程序崩溃。我通过将它们注释掉等方式将其缩小到这两行。

我完全不知道哪里出了问题。

【问题讨论】:

  • “应用程序崩溃”是什么意思?以什么方式?你得到什么堆栈跟踪?
  • 在模拟器中运行时只是主循环硬崩溃
  • 您需要获取回溯(在调试控制台中键入 bt),“崩溃”不会帮助任何人解决您的问题
  • 哦,你确实需要那个被注释掉的版本,否则你会泄露原来的 components 分配。

标签: objective-c nsdate nstimeinterval


【解决方案1】:

由于您的崩溃堆栈跟踪位于 _CFAutoreleasePoolPop 内的 objc_msgSend 中,因此您可以推断它可能是一个过度释放的错误。

这一行是错误的:

[components release], components=nil;

您不拥有那里的组件。查看提供给您的方法的名称。你过度释放它。

【讨论】:

    【解决方案2】:

    正如 rob mayoff 所说,第二个版本是错误的(而第一个版本是必要的)。试试这个:

    + (NSArray *)currentReservations {
        NSDate *today = [NSDate date];
        NSCalendar *gregorian = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
        NSDateComponents *components = [[[NSDateComponents alloc] init] autorelease];
        [components setDay:1];  // tomorrow
        NSDate *tomorrow = [gregorian dateByAddingComponents:components toDate:today options:0];
        NSUInteger unitFlags = NSYearCalendarUnit | NSMonthCalendarUnit | NSDayCalendarUnit;
        components = [gregorian components:unitFlags fromDate:tomorrow];
        [components setHour:0];
        [components setMinute:0];
        NSDate *tomorrowMidnight = [gregorian dateFromComponents:components];
        [gregorian release], gregorian=nil;
    
    
        NSTimeInterval interval = [today timeIntervalSince1970];
        NSTimeInterval tomorrowInterval = [tomorrowMidnight timeIntervalSince1970];
    
        NSArray *reservations = [Reservation findWithSql:@"select * from Reservation where timestamp < ? and timestamp > ?" withParameters:[NSArray arrayWithObjects:[NSNumber numberWithDouble:tomorrowInterval], [NSNumber numberWithDouble:floor(interval)], nil]];
        return reservations;
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-05-12
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多