我一定是做错了什么,但是 Automatic Reference Counting 文档并没有给我提示它可能是什么。我正在做的是从委托方法内部调用一个带有块回调的方法。从块内部访问同一个委托会导致访问错误。问题是我正在传递的对象 - loginController将消息发送给它的委托 - 显然没有释放,当我不在块内访问它时,我可以多次调用该方法而不会出现问题。这是我的代码:
- (void)loginViewDidSubmit:(MyLoginViewController *)loginController
{
NSString *user = loginController.usernameLabel.text;
NSString *pass = loginController.passwordLabel.text;
__block MyLoginViewController *theController = loginController;
[self loginUser:user withPassword:pass callback:^(NSString *errorMessage) {
DLog(@"error: %@", errorMessage);
DLog(@"View Controller: %@", theController); // omit this: all good
theController = nil;
}];
}
NSZombieEnabled 不记录任何内容,并且 gdb 没有可用的堆栈跟踪。我在这里做错了什么?感谢您的任何指点!
编辑:
我认为问题的范围更大——上面的回调是从 NSURLConnectionDelegate 方法调用的(块本身是该委托的强属性,因此 ARC 应该调用 Block_copy())。在这种情况下我需要进行特殊测量吗?
流程(loginController 始终可见):
登录控制器
[delegate loginViewDidSubmit:self];
查看代表
(method shown above calls the loginUser: method, which does something like:)
httpDelegate.currentCallback = callback;
httpDelegate.currentConnection = // linebreak for readability
[[NSURLConnection alloc] initWithRequest:req
delegate:httpDelegate
startImmediately:YES];
NSURLConnectionDelegate
- (void)connection:(NSURLConnection *)aConnection
didFailWithError:(NSError *)error
{
if (NULL != currentCallback) {
currentCallback([error localizedDescription]);
self.currentCallback = NULL;
}
}
这就是我获得错误访问权限的地方,但只有当我访问该 loginController 变量时...