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
}

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

1 回答 1

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];
于 2012-09-27T21:44:22.593 回答