2012-04-23 2 views
2

Я использовал утечки, чтобы найти утечку памяти, связанную с SBJsonParser, и я не понимаю, почему я ее получаю? Я надеялся, что кто-то сможет пролить некоторое понимание. Утечки сообщают, что утечка исходит от метода, называемого objectWithURL. Этот метод вызывается из метода, называемого downloadJSONFeed. Я показал оба ниже.SBJsonParser Memory Leak

Любое понимание оценено.

- (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, похоже, исправил проблему. Спасибо. –

0

Простите, что прошло некоторое время с тех пор, как я закодировал Objective-C, но это работает?

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

Yup. Необходимо освободить парсер. Храните синтаксический анализ в стеке var, отпустите парсер и верните его.