2012-04-23 28 views
2

我用泄漏找到與SBJsonParser相關的內存泄漏我不明白爲什麼我得到它?我希望有人能夠提供一些見解。 泄漏報告泄漏來自名爲objectWithURL的方法。該方法是從名爲downloadJSONFeed的方法調用的。我已經在下面顯示。SBJsonParser內存泄漏

任何洞察讚賞。

- (id) objectWithUrl:(NSURL *)url 
{ 

    SBJsonParser *jsonParser = [SBJsonParser new]; 
    NSString *jsonString = [self stringWithUrl:url]; 

    // Parse the JSON into an Object 
    return [jsonParser objectWithString:jsonString error:NULL]; 

} 

- (void) downloadJSONFeed 
{ 

    //set up query 
    NSString *lat = [NSString stringWithFormat:@"%f", ad.locationManager.location.coordinate.latitude]; 
    NSString *lon = [NSString stringWithFormat:@"%f", ad.locationManager.location.coordinate.longitude]; 
    NSString *postValues = [NSString stringWithFormat:@"http://vivid-wind-8436.herokuapp.com/bathrooms/nearby.json/?lat=%@&lon=%@",lat, lon]; 


    //get server response 
    id response = [self objectWithUrl:[NSURL URLWithString:postValues]]; 

    //store in dictionary 
    NSDictionary *dictionary = (NSDictionary *)response; 

    //array for json data 
    NSMutableArray *jsonData = [[NSMutableArray alloc] init]; 

    for (NSDictionary *dict in dictionary) 
    { 
     Bathroom *bathroom = [[[Bathroom alloc] init] autorelease]; 
     bathroom.name = [dict objectForKey:@"name"]; 
     bathroom.street = [dict objectForKey:@"street"]; 
     bathroom.city = [dict objectForKey:@"city"]; 
     bathroom.state = [dict objectForKey:@"state"]; 
     bathroom.postal = [dict objectForKey:@"postal"];   
     bathroom.country = [dict objectForKey:@"country"]; 
     bathroom.accessibility = [dict objectForKey:@"access"]; 
     bathroom.gendered = [dict objectForKey:@"bathroomtype"]; 
     bathroom.availability = [dict objectForKey:@"avail"]; 
     bathroom.directions = [dict objectForKey:@"directions"]; 
     bathroom.comment = [dict objectForKey:@"comment"]; 
     bathroom.distance = [dict objectForKey:@"distance"]; 
     bathroom.lat = [dict objectForKey:@"lat"]; 
     bathroom.lon = [dict objectForKey:@"lon"]; 

     [jsonData addObject:bathroom]; 
    } 

    //now sort array by distance 
    NSSortDescriptor *sortDescriptor; 
    sortDescriptor = [[[NSSortDescriptor alloc] initWithKey:@"distance"             ascending:YES] autorelease]; 
    NSArray *sortDescriptors = [NSArray arrayWithObject:sortDescriptor]; 
    NSArray *sortedArray; 
    sortedArray = [jsonData sortedArrayUsingDescriptors:sortDescriptors]; 
    //dataArray = [[NSMutableArray alloc] init]; 

    //add objects to data array 
    [dataArray addObjectsFromArray:sortedArray]; 


    //release json data 
    [jsonData release]; 

}  

回答

1

[SBJsonParser new]等於[[SBJsonParser alloc] init]。通過調用這個objectWithUrl擁有的創建SBJsonParser對象,所以你需要在此方法中釋放它:

SBJsonParser *jsonParser = [[SBJsonParser new] autorelease]; 

你還可以:

- (id)objectWithUrl:(NSURL *)url 
{ 
    SBJsonParser *jsonParser = [SBJsonParser new]; 
    NSString *jsonString = [self stringWithUrl:url]; 

    // Parse the JSON into an Object 
    id jsonObject = [jsonParser objectWithString:jsonString error:NULL]; 
    [jsonParser release]; 

    return jsonObject; 
} 

參考Another iPhone Memory leak issue

+0

autorelease似乎已經解決了這個問題。謝謝。 – 2012-04-23 17:47:38

0

對不起它已經有一段時間,因爲我在Objective-C語言編寫的,但工作的呢?

SBJsonParser *jsonParser = [[[SBJsonParser alloc] init] autorelease]; 
0

是。需要釋放解析器。將解析存儲在堆棧var中,釋放解析器,然後返回。