Objective-C ARC - does a method retain 'self'? [duplicate]

岁酱吖の 提交于 2019-12-11 03:12:57

问题


Is this a safe thing to do?

__weak typeof (self) welf = self;
dispatch_async(dispatch_get_main_queue(), ^{
    [welf doStuff];
})

....

-(void)doStuff {
    [_member1 someMethod];
    ....
    [_member2 someMethodWithParam:_someCArray[1];];
}

As far as I understand ARC, the actual call to [welf doStuff] should be fine, as welf will either be valid (and the method call will proceed), or it will be nil - calling any method on nil is defined safe behaviour in Objective-C so that seems ok.

Once we get into doStuff however, does ARC cause self to be retained for the duration of the method call? If it does, I think we should be ok... If not, then self could get dealloc'ed halfway through the method call and then we'd be in a bad spot.

Note: I googled for this but couldn't find anything obvious. Perhaps it's just hard to search for?

Thanks!


回答1:


You need to create a strong reference to self inside a block and call a method on it.

    __weak typeof (self) weakSelf = self;
    dispatch_async(dispatch_get_main_queue(), ^{
        typeof(self) strongSelf = weakSelf;
        [strongSelf doStuff];
    })

And yes, Orion, your question is answered by this question itself, and this particular anwswer




回答2:


As it turns out I found the answer eventually, both here: https://stackoverflow.com/a/18011581/234 and here: http://clang.llvm.org/docs/AutomaticReferenceCounting.html#self

self is always unsafe unretained under ARC, so in my example, it would NOT be safe, and there would be nothing to stop it getting deallocated partway through my doStuff method. Ouch



来源:https://stackoverflow.com/questions/32468758/objective-c-arc-does-a-method-retain-self

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!