有人能解释一下为什么对象引用在末尾有计数器1吗?
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)调用alloc/init(工厂方法),返回自动释放对象给调用者
- (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)作为参数传递给block
__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) ? ?(可能是block release)
10)从属性释放
[currentMainRequest release];
根据我的理解,第一个malloc保持不平衡,但第一个malloc创建对象,它总是+1 !!
块保留它们引用的对象。
7 Retain +1 2 00:03.412.608 libsystem_sim_blocks.dylib _Block_object_assign
尝试在变量旁边使用__block
关键字,以防止它被块保留。