为什么对象泄露(目标C)

时间:2014-06-05 21:26:41

标签: objective-c memory-leaks

有人可以解释为什么对象引用最后有计数器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(工厂方法),将autorelease对象返回给调用者

- (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)??? (可能阻止释放)

10)从财产中释放

 [currentMainRequest release];

至于我的理解,第一个malloc仍然是不平衡的,但是第一个malloc创建对象,它始终是+1 !!!

1 个答案:

答案 0 :(得分:0)

块保留它们引用的对象。

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

尝试在变量旁边使用__block关键字,以防止该块保留它。