昨天我提出了一个关于内存管理和单例的问题(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,所以当我去从我的单身删除对象它会导致内存泄漏。唯一可以总结的是,我遇到了一些与块有关的内存问题。如果有人能提供一些见解,将非常感谢!
嗯,我已经使用了泄漏工具,它提供了一点洞察力。至于retaincount,我昨天使用了这个功能来查找一个bug。 –
'retainCount'无用;总有一种更好的方式。 – bbum
bbum非常感谢你的评论:) –