2

リークを使用して、SBJsonParserに関連するメモリリークを特定しましたが、なぜそれが発生するのかわかりませんか?私は誰かがいくつかの洞察を流すことができるかもしれないことを望んでいました。Leaksは、リークが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];

}    
4

3 に答える 3

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;
}

別のiPhoneメモリリークの問題を参照してください。

于 2012-04-23T16:21:59.707 に答える
0

申し訳ありませんが、Objective-Cでコーディングしてからしばらく経ちましたが、これは機能しますか?

SBJsonParser *jsonParser = [[[SBJsonParser alloc] init] autorelease];
于 2012-04-23T16:19:28.987 に答える
0

うん。パーサーを解放する必要があります。解析をスタック変数に格納し、パーサーを解放してから戻ります。

于 2012-04-23T16:20:00.690 に答える