iphone - SBJsonParser 内存泄漏

标签 iphone objective-c ios memory-management sbjson

我已经使用泄漏来定位与 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];

}    

最佳答案

[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 .

关于iphone - SBJsonParser 内存泄漏,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10284204/

相关文章:

iphone - 在整个 iOS 应用程序中共享字符串和变量

objective-c - 如何在 OSX 中激活外部应用程序的特定窗口?

ios - 适用于 iOS 的类似 Tinder 的滑动动画

ios - 当我在 UITextView 委托(delegate)方法 textViewDidChange 中设置属性文本时如何停止光标更改位置

iphone - 如何创建一个画廊来使用滑动手势显示照片?

jquery - 获取真实的 iPhone 分辨率 - jquery

iphone - 在iPhone上,如何制作图标徽章?

iphone - 做 git status, objective c, git 时总是得到 Crashlytics is modified

ios - 当我调整模态视图 Controller 大小时,我有一个阴影错误

iphone - 如何从 JSON 中获取 bool 值?