2014-06-05 28 views
2

有人可以解释为什么对象引用最后有计数器1吗?为什么对象被泄露(目标C)

5 Malloc +1 1 00:03.412.584 Control -[PaymillPaymentService createServiceRequestWith:and:] 
6 Autorelease   00:03.412.596 Control -[PaymillPaymentService createServiceRequestWith:and:] 
7 Retain +1 2 00:03.412.608 libsystem_sim_blocks.dylib _Block_object_assign 
8 Retain +1 3 00:03.412.620 Control -[DashboardViewController requestMainTransactionsList] 
9 Release -1 2 00:03.506.116 UIKit _UIApplicationHandleEvent 
10 Release -1 1 00:04.104.252 Control -[DashboardViewController transactionListLoadingComplete:] 

5,6)呼叫分配/初始化(工厂方法),返回自动释放物体至呼叫者

- (ServiceRequest*)createServiceRequestWith:(NSString*)url and:(id)delegate 
{ 
    NSURL *fullURL = [NSURL URLWithString:url]; 
    NSMutableURLRequest *request = [[[NSMutableURLRequest alloc] initWithURL:fullURL] autorelease]; 
    [NSMutableURLRequest basicAuthForRequest:request withUsername:[self accessToken] andPassword:@""]; 

    ServiceRequest *serviceRequest = [[ServiceRequest alloc] initWithURLRequest:request forDelegate:delegate]; 

    return [serviceRequest autorelease]; 
} 

7)传作为参数对块

__unsafe_unretained id blockRequest = serviceRequest; 
[operation setCompletionBlock:^{ 
    [self handleTransactionListRequest:blockRequest]; 

}]; 

8)保持分配以强劲的财产

ServiceRequest* request = [[[AppController sharedController] currentService] retrieveTransactionListInto:transactionList usingInterval:timeInterval forDelegate:self]; 
request.tag = @"main"; 
self.currentMainRequest = request; 

9)??? (可能是块释放)物业

[currentMainRequest release]; 

至于我的理解

10)发布,第一的malloc仍然不平衡,但首先malloc的创建对象时,它始终是+1!

回答

0

块保留它们引用的对象。

7 Retain +1 2 00:03.412.608 libsystem_sim_blocks.dylib _Block_object_assign 

尝试使用__block关键字沿着变量,以防止它从由所述块被保留。

+0

这是一个很好的评论。增加__block但目前我的对象被基本上立即释放,尽管其他保留...调查进一步... –

+0

另外,我实际上需要在块的这个对象,可以吗? –

+1

你是对的,在我以其他方法移动autorelease后,该对象被正确释放。所以基本上在我的原始文本中,7)块保留是不平衡的。谢谢! –