ARC:从代理方法中使用的块中获取EXC_BAD_ACCESS [英] ARC: Getting EXC_BAD_ACCESS from inside block used in delegate method

查看:92
本文介绍了ARC:从代理方法中使用的块中获取EXC_BAD_ACCESS的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我必须做错事,但自动参考计数文件不会给我一个提示,它可能是什么。我正在做的是从代理方法中调用具有块回调的方法。 从块内访问同一个代理会导致访问不良。问题是我传递的对象 - loginController 这是发送消息到它的委托 - 显然不是释放,当我不访问它在块内我可以多次调用该方法,而不是一个问题。这里是我的代码:

I must be doing something wrong, but the Automatic Reference Counting docs don't give me a hint on what it might be. What I'm doing is calling a method with a block callback from inside a delegate method. Accessing that same delegate from inside the block results in a bad access. The problem is the object I'm passing - loginController which is sending the message to its delegate - is clearly not released, when I don't access it inside the block I can call the method multiple times without an issue. Here's my code:

- (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的可用堆栈跟踪。我在这里做错了什么?感谢任何指针!

NSZombieEnabled does not log anything and there is no usable stack trace from gdb. What am I doing wrong here? Thanks for any pointers!

编辑

我想这个问题有一个更大的范围 - 上面的回调从NSURLConnectionDelegate方法(块本身是一个强大的属性,因此ARC应该调用Block_copy())。我需要在这种情况下进行特殊测量吗?

I figured the problem has a bigger scope - the callback above is called from an NSURLConnectionDelegate method (the block itself is a strong property for that delegate so ARC should call Block_copy()). Do I need to take special measurements in this scenario?

Flow(loginController始终可见):

Flow (the loginController stays visible all the time):

[delegate loginViewDidSubmit:self];



查看代表



View Delegate

(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



NSURLConnectionDelegate

- (void)connection:(NSURLConnection *)aConnection
  didFailWithError:(NSError *)error
{
    if (NULL != currentCallback) {
        currentCallback([error localizedDescription]);
        self.currentCallback = NULL;
    }
}

这是我访问错误的地方,只有当我访问loginController变量...

And this is where I get the bad access, but ONLY if I access that loginController variable...

推荐答案

设置拷贝属性到属性,

Set copy attribute to the property, or just call 'copy' method for the block.

- (void)loginUser:(NSString *)user withPassword:(NSString *)pass callback:(void (^callback)(NSString *))
{
    callback = [callback copy];

这篇关于ARC:从代理方法中使用的块中获取EXC_BAD_ACCESS的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆