块和内存管理

时间:2011-08-11 15:32:09

标签: objective-c ios cocoa-touch

昨天我提出了一个关于内存管理和单身人士的问题(Proper Management Of A Singleton Data Store In IOS With Web Service)。在过去的36个小时里,我一直在试图追踪问题,经过NSLog等的大量测试后,我只能得出结论,块内分配的对象没有被自动释放。我使用块作为处理异步Web服务响应的方法。我还从视图控制器发送一个块,该块需要发出Web服务请求,以便它可以根据Web服务的响应执行任何操作。希望全面了解我的代码将有助于获得解决方案,我将把所有产生问题的代码放在这里:

第一步是从我的根视图控制器发出请求。当它加载时,我请求所有用户的“Hollers”一个我在内部使用的术语,实质上意味着事件。当根视图控制器加载时,我调用一个包含以下代码的方法:

HollerWebService *api = [[HollerWebService alloc]init];
//NSLog(@"About to get the hollers for userId: %@", [[[CurrentSession defaultStore]currentUser]userId]);
[api getLatestHollers:[[[CurrentSession defaultStore]currentUser]userId] completionBlock:^(BOOL succeeded) {
    //If the user has 0 hollers, display one of the illustrations
    [self.tableView reloadData];
    [self stopLoading];
    if( !succeeded ){
        UIAlertView *av = [[UIAlertView alloc] initWithTitle:@"Error" message:@"Could not connect to Holler" delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil];
        [av show];
        [av release];
    }
    else
    {
        for( Holler *hN in [[HollerStore defaultStore] allHollers] )
        {
            //Retain count of all the hollers is 
            NSLog(@"Retain count of the holler is %i", [hN retainCount]);
        }
    }
}];

[api release];

正如您所看到的,我有一个注释区域,它将保留计数打印到NSLog,这是我调试过程的一部分。此时,它告诉我每个对象的保留计数为2,这意味着在下一组代码中生成的原始Holler尚未自动释放。下一组代码在我的实际Web服务中。我已经在此方法的关键部分中添加了注释,这会导致保留计数增加:

- (void)getLatestHollers: (NSString *)userId completionBlock:(void (^)(BOOL succeeded))handler
{
    [self getRequest:[[NSString alloc]initWithFormat:@"hollers/feed?user_id=%@",userId] completionBlock:^(NSData *receivedData, NSURLResponse *receivedResponse, NSError *error) {
    if( error == nil )
    {
        SBJsonParser *json = [SBJsonParser new];
        NSArray *response = [json objectWithData:receivedData];
        [json release];
        //NSLog(@"Got the latest Hollers. %@", response);
        HollerStore *hStore = [HollerStore defaultStore];
        for( NSDictionary *holler in response )
        {
            //At this point Holler *h is being sent an autoreleased holler
            //from the parseHoller: method.  At this point it's retain count is 1
            Holler *h = [self parseHoller:holler];
            [hStore addHoller:h];
            //Now that I've added it to my singleton the retain count is 2, although I'm 
            //assuming the autorelease pool will eventually come through and reduce this 
            //to 1 but it never happens
        }
        handler(YES);
    }
    else
    {
        NSLog(@"The API failed :(, %@", [error localizedDescription]);
        //Let the requestor know that this request failed
        handler(NO);
    }
    }];
}

此时保留计数保持为2而不是1,所以当我从单例中删除对象时,会导致内存泄漏。我唯一可以得出的结论是,我遇到了一些与块有关的内存问题。如果有人能提供一些见解,那将非常感谢!!!

1 个答案:

答案 0 :(得分:2)

你根本不应该使用retainCount永远 - 真的Apple应该从SDK中删除它,因为它根本没用。查看这个问题是出于技术原因,为什么你不应该使用它点空白:

When to use -retainCount?

我将跳到原来的问题,有人发布你应该使用它并指出这一点。你可能有内存泄漏,但保留计数不是用来解决它的正确方法!