2011-11-02 84 views
1

我似乎無法找出一段代碼更改的錯誤,導致我的應用程序中出現硬故障,而沒有多少調試線索。NSTimeInterval無法解釋的崩潰

這裏是原來的方法

+ (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]; 

包括應用程序崩潰。我已經通過註釋出來,等把範圍縮小到這兩條線..

我在什麼是錯的損失是完全。

+0

你所說的「應用程序崩潰」呢?以什麼方式?你有什麼堆棧跟蹤? – Jim

+0

在模擬器只是一個硬碰撞到主循環 – cpjolicoeur

+1

運行時,你需要得到一個回溯(BT型在調試控制檯),「崩潰」是不會幫助任何人解決您的問題 – jrturton

回答

1

因爲你的崩潰堆棧跟蹤是在objc_msgSend裏面_CFAutoreleasePoolPop,你可以推斷,它可能是過度釋放錯誤。

此行是錯誤的:

[components release], components=nil; 

你沒有自己的組件存在。看看給你的方法的名稱。你過度釋放它。

0

正如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; 
}