潜在的泄漏问题

时间:2012-09-27 20:19:15

标签: iphone ios memory-management

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

潜在的泄漏即将到来......
这是我的代码

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

我试过这样做 return [self 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;
}

1 个答案:

答案 0 :(得分:0)

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

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

如果您对生成的autorelease NSMutableArray进行了 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];
{{1}}