NSDateFormatter内存泄漏?(NSDateFormatter memory leak?)

2019-10-30 10:47发布

我有一个问题, NSDateFormatter dateFromString:方法。 有字典与日期字符串数组,我想将它们转换为NSDates 。 但是,有一个巨大的堆增长我每次调用此方法时,(我需要调用它不止一次,因为它是一个“更新”的方法)。 此外,我把它称为后台线程和ARC上。 我究竟做错了什么? 任何帮助,请。

UPDATE:函数的完整代码:

- (GraphController *) initGraphWithData: (NSArray *) points forInterval: (GraphInterval) interval andFrame: (CGRect) frame gestures: (BOOL) gestures secondGraph: (NSArray *) secondPoints graphNames: (NSArray *) graphNames

{
self = [super init];

_calendar = [NSCalendar currentCalendar];

_secondPoints = secondPoints;
_graphNames = graphNames;

[self.view setFrame:frame];

_points = [[NSMutableArray alloc] init];
double minValue = HUGE_VALF, maxValue = -HUGE_VALF;
NSDate *minDate = [NSDate date], *maxDate = [NSDate dateWithTimeIntervalSince1970:0];
NSMutableDictionary *datesTable = [[NSMutableDictionary alloc] init];          


int index = 0;
for(NSArray *pointArray in points){

    NSDictionary *point = pointArray[0];
    NSMutableDictionary *newPoint = [[NSMutableDictionary alloc] initWithDictionary:point];


    // convert date        
    NSString *dateString = (NSString *)[point objectForKey:@"date"];
    NSLog(@"dateString to convert: %@", dateString);
    [_dateFormatter setDateFormat:@"MM/dd/yyyy"];
    NSDate *date = [_dateFormatter dateFromString: dateString];
    [newPoint setObject: date forKey:@"date"];


    // convert numbers
    [newPoint setObject: [NSNumber numberWithFloat:[((NSString *)[point objectForKey:@"value"]) floatValue]] forKey:@"value"];
    if(secondPoints)
        [newPoint setObject: [NSNumber numberWithFloat:[((NSString *)[secondPoints[index] objectForKey:@"value"]) floatValue]] forKey:@"secondValue"];
    [newPoint setObject: [NSNumber numberWithInt:index]  forKey:@"index"];

    // min and max
    if([[newPoint objectForKey:@"value"] floatValue] < minValue)
        minValue = [[newPoint objectForKey:@"value"] floatValue];
    if([[newPoint objectForKey:@"value"] floatValue] > maxValue)
        maxValue = [[newPoint objectForKey:@"value"] floatValue];

    // check second value
    if(self.secondPoints){            
        if([[newPoint objectForKey:@"secondValue"] floatValue] < minValue)
            minValue = [[newPoint objectForKey:@"secondValue"] floatValue];
        if([[newPoint objectForKey:@"secondValue"] floatValue] > maxValue)
            maxValue = [[newPoint objectForKey:@"secondValue"] floatValue];
    }

    if([[newPoint objectForKey:@"date"] timeIntervalSince1970] > [maxDate timeIntervalSince1970])
        maxDate = [newPoint objectForKey:@"date"];
    if([[newPoint objectForKey:@"date"] timeIntervalSince1970] < [minDate timeIntervalSince1970])
        minDate = [newPoint objectForKey:@"date"];

    [self.points addObject:newPoint];
    [datesTable setObject:newPoint  forKey: [[NSNumber numberWithDouble:[date timeIntervalSince1970]] stringValue] ];

    index++;            

}




// set draw parameters

_drawVars = [[NSMutableDictionary alloc] init];

NSDate *startSearchDate;
switch (interval) {
    case GraphIntervalWeek:
        startSearchDate = [maxDate dateByAddingTimeInterval:-7*24*3600];
        break;
    case GraphIntervalMonth:
        startSearchDate = [maxDate dateByAddingTimeInterval: -31*24*3600];
        break;
    case GraphIntervalSixMonths:
        startSearchDate = [maxDate dateByAddingTimeInterval:-6*31*24*3600];
        break;
    case GraphIntervalYear:
        startSearchDate = [maxDate dateByAddingTimeInterval:-365*24*3600];
        break;
    case GraphIntervalAllTime:
        break;
    default:
        break;
}

NSMutableDictionary *searchPoint;

while(searchPoint == nil && [startSearchDate timeIntervalSince1970] > [minDate timeIntervalSince1970]){
    searchPoint = [datesTable objectForKey:[[NSNumber numberWithDouble:[startSearchDate timeIntervalSince1970]] stringValue]];
    startSearchDate = [startSearchDate dateByAddingTimeInterval:-24*3600];
}

if(searchPoint != nil && interval != GraphIntervalAllTime)
 [self.drawVars setObject:[searchPoint objectForKey:@"index"] forKey:@"startDrawIndex"];
else
 [self.drawVars setObject:[NSNumber numberWithInt:0] forKey:@"startDrawIndex"];

[self.drawVars setObject:[[NSNumber numberWithFloat:minValue] copy] forKey:@"minValue"];
[self.drawVars setObject:[[NSNumber numberWithFloat:maxValue] copy] forKey:@"maxValue"];
[self.drawVars setObject:minDate forKey:@"minDate"];
[self.drawVars setObject:maxDate forKey:@"maxDate"];
[self.drawVars setObject:datesTable forKey:@"datesTable"];



// set drawImageView
_drawArea = [[UIImageView alloc] initWithFrame: frame];
[self.view addSubview:self.drawArea];


// set overlayImageView for fingerInpect
_inspectOverlay = [[UIImageView alloc] initWithFrame:frame];
[self.view addSubview:self.inspectOverlay];

// set hintUIView for fingerInspect
_hint = [[Hint alloc] initWithFrame:frame];
[self.hint initHint];
self.hint.hidden = YES;
[self.drawArea addSubview:self.hint];    



UIPanGestureRecognizer *panRecognizer = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(panHandler:)];
UILongPressGestureRecognizer *longPressRecognizer  = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(longPressHandler:)];    


if(gestures)
    [self.view addGestureRecognizer:panRecognizer];
[self.view addGestureRecognizer:longPressRecognizer];

return self;
}

Answer 1:

我看你正在使用Heapshot分析。 好。 这里有一些更多的细节:

http://www.friday.com/bbum/2010/10/17/when-is-a-leak-not-a-leak-using-heapshot-analysis-to-find-undesirable-memory-growth/

现在,希望你Heapshot迭代的每一个代表在做您的应用程序的东西,应该将其返回到基态。 地打开和关闭一个文件,例如。

如果是这样的话,你的数据显示每次迭代一个500K-600K的增长。 这是相当多的。

需要注意的是要分析的最后一次迭代一般是无用的; 许多这些物体会存在,因为无论应用程序的当前状态。 你真的想专注于2或4迭代。

至于为什么该特定日期或日期格式漏水,打开引用计数追踪事件,然后查看保留/释放事件的泄漏对象之一。 会有一些数量的额外保留。



Answer 2:

你正在做正确的事情是initNSDateFormatter进入循环前。 如果你期望在同一日期格式( MM/dd/yyyy ),你可以把你在同一个地方init它。 它可以是你分配过多的对象,所以我建议把一切内部@autoreleasepool {}又名:自动释放内部循环的内容)。

我看到你在分配*newPoint对里面的,然后你设置日期到该对象。 你是做什么之后?



文章来源: NSDateFormatter memory leak?