错误:无法在iOS中分配区域

时间:2013-08-09 09:24:22

标签: iphone ios objective-c memory-leaks

每当通过调用此方法选择UIPickerView行时,我就会将对象添加到可变数组中 -

-(void)setScheduleStartDate:(NSString *)dateStr
{
    [scheduleDatesArray removeAllObjects];
    NSDateFormatter* df = [[NSDateFormatter alloc] init];
    df.dateFormat = @"d MMMM yyyy";
    scheduleStartDate = [df dateFromString:dateStr];

    /******* getting array of schedule dates     ***********/

    NSDate* scheduleEndDate = [scheduleStartDate dateByAddingTimeInterval:60*60*24*28*6]; // add six month (of 28 days) in schedule start date
    double endMS = [scheduleEndDate timeIntervalSinceDate:scheduleStartDate];

    for (double i =0; i < endMS; i = (i + 60*60*24*14)) {

        [scheduleDatesArray addObject:[NSNumber numberWithDouble:i]];
    }
}

在多次调用此方法后,我遇到此错误消息

malloc: *** mmap(size=627101696) failed (error code=12)
*** error: can't allocate region
*** set a breakpoint in malloc_error_break to debug

通过设置malloc_error_break中的断点,我的应用程序中断for循环(我将对象添加到数组中)。但我无法找到问题,我搜索了谷歌同样的问题,但仍然没有运气。

任何人都能帮我解决我做错的事吗?

1 个答案:

答案 0 :(得分:2)

你真的不应该根据假设一个月是28天甚至是a minute is 60 seconds进行日期计算。

而不是dateByAddingTimeInterval:使用NSCalendar的{​​{1}}

dateByAddingComponents:toDate:options:

编辑:根据我的理解,您希望每隔~14天为您的阵列添加一个时间间隔(将您定义为一个月为28天的半个月)。为了更加健壮,我会做这样的事情(没有经过测试但应该有效,请告诉我):

NSDateComponents *sixMonthComponents = [[NSDateComponents alloc] init];
sixMonthComponents.month = 6;

NSCalendar *currentCalendar = [NSCalendar currentCalendar];
NSDate* scheduleEndDate = [currentCalendar dateByAddingComponents:sixMonthComponents toDate:scheduleStartDate options:0];

旁注// We increment i with the total interval between the two dates (endMS) divided by 12 (6 months * 2 times per month) for (double i = 0; i < endMS; i += (endMS / 12) { [scheduleDatesArray addObject:@(i)]; } can be written @(i)