2012-09-27 95 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