1

デバッグの痕跡がほとんどないと、アプリでハード クラッシュを引き起こしているコード変更のバグを特定できないようです。

元の方法はこちら

+ (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 時間先までのタイムスタンプを持つすべてのレコードを見つけることができます。現在と明日の間 (翌日の午前 0 時) のタイムスタンプを持つすべてのレコードをクエリするようにメソッドを変更する必要があるため、この他のスタックオーバーフローの質問に基づいてコードを更新しました

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

ただし、次の 2 行の場合:

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

アプリのクラッシュが含まれています。コメントアウトするなどして、この2行に絞り込みました。

私は何が悪いのか完全に途方に暮れています。

4

2 に答える 2

1

クラッシュ スタック トレースは _CFAutoreleasePoolPop 内の objc_msgSend にあるため、おそらくオーバーリリース バグであると推測できます。

この行は間違っています:

[components release], components=nil;

そこにコンポーネントを所有していません。あなたにそれを与えたメソッドの名前を見てください。あなたはそれを過剰に解放しています。

于 2011-11-02T16:44:10.377 に答える
0

rob mayoffが言ったように、2番目のリリースは間違っています(最初のリリースは必要ですが)。代わりにこれを試してください:

+ (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;
}
于 2011-11-02T16:52:05.923 に答える