2012-09-27 96 views
1

首先请告诉我什么是潜在的泄漏以及它为什么会发生。潜在的泄漏问题

潜在的泄漏来了...
这里是我的代码

-(NSArray*)calculateRoutesFrom:(CLLocationCoordinate2D) f to: (CLLocationCoordinate2D) t 
{ 
    //NSLog(@"maproute....5.."); 

    NSString* saddr = [NSString stringWithFormat:@"%f,%f", f.latitude, f.longitude]; 
    NSString* daddr = [NSString stringWithFormat:@"%f,%f", t.latitude, t.longitude]; 
    NSError* error; 

    NSString* apiUrlStr = [NSString stringWithFormat:@"http://maps.google.com/maps?output=dragdir&saddr=%@&daddr=%@&dirflg=w", saddr, daddr]; 
    NSURL* apiUrl = [NSURL URLWithString:apiUrlStr]; 
    NSLog(@"api url: %@", apiUrl); 

    NSString *apiResponse = [NSString stringWithContentsOfURL:apiUrl encoding:NSUTF8StringEncoding error:&error]; 
    NSLog(@"what is this"); 
    NSString* encodedPoints = [apiResponse stringByMatching:@"points:\\\"([^\\\"]*)\\\"" capture:1L]; 

    NSLog(@"maproute....4.."); 


    return [self decodePolyLine:[encodedPoints mutableCopy]]; // Here is The potential leak 
} 

我试着这样做 返回[自我decodePolyLine:[encodedPoints mutableCopy]自动释放]。 但它发送到很多时间autorelease消息。

怎么办请帮忙。

-(NSMutableArray *)decodePolyLine: (NSMutableString *)encoded { 
    [encoded replaceOccurrencesOfString:@"\\\\" withString:@"\\" 
           options:NSLiteralSearch 
           range:NSMakeRange(0, [encoded length])]; 
    NSInteger len = [encoded length]; 
    NSInteger index = 0; 
    NSMutableArray *array = [[[NSMutableArray alloc] init] autorelease]; 
    NSInteger lat=0; 
    NSInteger lng=0; 
    while (index < len) { 
     NSInteger b; 
     NSInteger shift = 0; 
     NSInteger result = 0; 
     do { 
      b = [encoded characterAtIndex:index++] - 63; 
      result |= (b & 0x1f) << shift; 
      shift += 5; 
     } while (b >= 0x20); 
     NSInteger dlat = ((result & 1) ? ~(result >> 1) : (result >> 1)); 
     lat += dlat; 
     shift = 0; 
     result = 0; 
     do { 
      b = [encoded characterAtIndex:index++] - 63; 
      result |= (b & 0x1f) << shift; 
      shift += 5; 
     } while (b >= 0x20); 
     NSInteger dlng = ((result & 1) ? ~(result >> 1) : (result >> 1)); 
     lng += dlng; 
     NSNumber *latitude = [[[NSNumber alloc] initWithFloat:lat * 1e-5] autorelease]; 
     NSNumber *longitude = [[[NSNumber alloc] initWithFloat:lng * 1e-5] autorelease]; 
     printf("[%f,", [latitude doubleValue]); 
     printf("%f]", [longitude doubleValue]); 
     CLLocation *loc = [[[CLLocation alloc] initWithLatitude:[latitude floatValue] longitude:[longitude floatValue]] autorelease]; 
     [array addObject:loc]; 
    } 

    return array; 
} 
+0

你也可以显示什么'decodePolyLine:' – Alex

回答

0

您正确诊断问题,您的mutableCopy永远不会被释放。但你说[self decodePolyLine:[encodedPoints mutableCopy] autorelease]没有工作。但它不能,因为你在那里缺少一组括号。正确的解决方案,它执行autorelease的字符串,那就是:

return [self decodePolyLine:[[encodedPoints mutableCopy] autorelease]]; 

,如果你对得到的NSMutableArray做了autorelease你会得到你太多的版本,不过,通过这样的事情:

return [[self decodePolyLine:[encodedPoints mutableCopy]] autorelease]; 

这后一种使用autorelease是有问题的。但是,第一个提出的改变应该解决这个问题。

话虽如此,我建议进一步简化代码。我会对改变我传递给它们的可变参数的方法保持警惕(除非出于某种原因需要这种行为,在这里它似乎不是这种情况)。我宁愿看到:

-(NSMutableArray *)decodePolyLine:(NSString *)encodedInput { 
{ 
    NSString *encoded = [encodedInput stringByReplacingOccurrencesOfString:@"\\\\" 
                   withString:@"\\"]; 

    NSInteger len = [encoded length]; 

    // the rest of your method here 
} 

然后,你可以调用这个简称为:

return [self decodePolyLine:encodedPoints]; 

这也有巧合凭借完全消除问题的声明。


无关你的问题,你可以简化你的线条是说:

NSNumber *latitude = [[[NSNumber alloc] initWithFloat:lat * 1e-5] autorelease]; 
    NSNumber *longitude = [[[NSNumber alloc] initWithFloat:lng * 1e-5] autorelease]; 

是:

NSNumber *latitude = [NSNumber numberWithFloat:lat * 1e-5]; 
    NSNumber *longitude = [NSNumber numberWithFloat:lng * 1e-5]; 

甚至更​​好,也许退休NSNumber一共只有

float latitude = lat * 1e-5; 
    float longitude = lng * 1e-5; 
    printf("[%f,", latitude); 
    printf("%f]", longitude); 
    CLLocation *loc = [[[CLLocation alloc] initWithLatitude:latitude 
                longitude:longitude] autorelease]; 
+0

我认为根本问题是在这个方法的调用者过度释放 - 这就是为什么autorelease技术失败。 –

+0

不确定,但他提出的解决方案显然是无效的,上面的答案应该解决眼前的问题。 – Rob