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 に答える