2012-04-23 2 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가 문제를 해결 한 것으로 보입니다. 고맙습니다. –

0

죄송합니다. Objective-C로 코딩 한 이후로 죄송 합니다만이 작업을 수행합니까?

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

넵. 파서를 해제해야합니다. 파싱을 스택 var에 저장하고 파서를 해제 한 다음 반환하십시오.