是“自我"在ARC的方法中比较弱? [英] Is "self" weak within a method in ARC?

查看:98
本文介绍了是“自我"在ARC的方法中比较弱?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个偶尔崩溃的方法.

I have a method that occasionally crashes.

-(void)foo{
    [self doSomething];
    [self.delegate didFinish];
    [self doSomethingElse];
}

-doSomething可以正常工作,然后我调用委托-didFinish.在-didFinish中,对此对象的引用可能设置为nil,并在ARC下释放它.当方法崩溃时,它会在-doSomethingElse上执行此操作.我的假设是,自我将在方法中变得强大,从而使功能得以完成.自我是弱者还是强者?是否有相关文件?它是强还是弱的原因是什么?

-doSomething works correctly, then I call to a delegate -didFinish. Within -didFinish, the reference to this object might be set to nil, releasing it under ARC. When the method crashes, it does so on -doSomethingElse. My assumption was that self would be strong within a method, allowing the function to complete. Is self weak or strong? Is there documentation on this? What would the reasoning be for it being strong or weak?

修改

在受到以下一些答案的启发后,我进行了一些调查.在我的案例中,崩溃的真正原因是NSNotificationCenter在任何情况下都不会保留观察者. Mike Weller在下面指出,为了防止上述情况,方法的调用者应在调用对象时保留该对象,但是NSNotificationCenter似乎忽略了此问题,并始终对观察者保持弱引用.换句话说:

Upon being inspired by some of the answers below, I did some investigation. The actual cause of the crash in my case was that the NSNotificationCenter does not retain the observer in any case. Mike Weller indicates below that callers of methods should retain the object while it is being called in order to prevent the case that I described above, however it appears that NSNotificationCenter ignores this issue, and always maintains a weak reference to the observer. In other words:

-(void)setupNotification{
    //observer is weakly referenced when added to NSNotificationCenter
    [[NSNotificationCenter defaultCenter] addObserver:self
                                             selector:@selector(handleNotification:)
                                                 name:SomeNotification object:nil];
}

//handle the notification
-(void)handleNotification:(id)notification{
    //owner has reference so this is fine
    [self doSomething];
    //call back to the owner/delegate, owner sets reference to nil
    [self.delegate didFinish];
    //object has been dealloc'ed, crash
    [self doSomethingElse];
}

推荐答案

self在方法内会很强大,从而可以完成该功能.自我是弱者还是强者?

self would be strong within a method, allowing the function to complete. Is self weak or strong?

self在ARC中既不强也不弱.假定呼叫者拥有一个引用,并且 self是不安全的,未保留的.

self is neither strong nor weak in ARC. It is assumed that the caller holds a reference, and self is unsafe unretained.

self可以在ARC下使用其自己的方法进行-dealloc的确是正确的,并且程序执行此操作被视为未定义行为(或至少是危险的)".

It's also true that self can be -dealloced within its own method under ARC, and it's regarded as "Undefined Behavior (or at least dangerous)" for your program to do this.

强或弱的理由是什么?

What would the reasoning be for it being strong or weak?

对于 Performance (性能)并没有保留-避免(在大多数情况下)不必要的引用计数增加/减少.即使他们执行了所有这些额外的引用计数操作,您的程序仍然会在多线程程序中或存在竞争条件(也包括UB)的情况下易受此类问题的影响.因此,这是他们(正确地)确定不需要防御自己的那些极端情况之一.

It's unretained for Performance -- to avoid what is (in the vast majority of cases) an unnecessary reference count inc/dec. Even if they did all those extra ref count operations, your program would still be susceptible to such problems in multithreaded programs or in the presence of a race condition (also UB). So this is one of those extreme edge cases they (rightly) determined they need not defend themselves from.

是否有相关文件?

Is there documentation on this?

当然! :)

这篇关于是“自我"在ARC的方法中比较弱?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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