SBJsonParser内存泄漏

时间:2012-04-23 16:12:26

标签: iphone objective-c ios memory-management sbjson

我使用泄漏来查找与SBJsonParser相关的内存泄漏,因为我不明白我为什么会这样做?我希望有人能够提供一些见解。 泄漏报告泄漏来自一个名为objectWithURL的方法。从名为downloadJSONFeed的方法调用此方法。我在下面展示了两个。

有任何见解。

- (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];

}    

3 个答案:

答案 0 :(得分: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

答案 1 :(得分:0)

很抱歉,我在Objective-C中编码已经有一段时间了,但这有用吗?

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

答案 2 :(得分:0)

烨。需要释放解析器。将解析存储在堆栈var中,释放解析器,然后返回。