Why do we set delegate to nil at dealloc if the ob

2019-03-26 08:23发布

问题:

-(void) scrollViewDidScroll:(UIScrollView *)scrollView
{
    PO(NSStringFromCGPoint(self.tableView.contentOffset));
    PO(NSStringFromUIEdgeInsets(self.tableView.contentInset));

    while(false);
}

-(void)dealloc
{
    PO(NSStringFromClass([self class]));
    PO(@"Deallocated");
    self.tableView.delegate=nil;
}

Here I need to set self.tableView.delegate = nil to avoid error.

I am aware, from my previous question, that self.tableView.delegate won't automatically become nill when the delegate is destroyed. That's because the type of delegate is assign reference instead of weak reference.

However, what about self.tableView?

The only thing with strong reference to self.tableView is it's superview that's owned by self and self itsef.

So when self is destroyed, self.tableView should be destroyed too and that means self.tableView.delegate will be gone too.

So why do I need to set self.tableView.delegate=nil;

回答1:

If you hold the only reference to self.tableView, there's no need of setting the delegate to nil.

The only situation where you have to set the delegate to nil, if is another class has your class as a delegate, because if your class is destroyed, that other class will look for your class to implement some methods, and your call won't be there.



回答2:

You need to set delegate to nil in many cases. In your case tableView can be referenced by some external class and will not destroyed after your class dealloc method. And will continue call its delegate method leading to crash. There are several classes that works in another thread (NSURLConnection for example). Even if you release it it can continue calls delegate methods since it is retained in another thread.



回答3:

Lets say we have a SpriteKit scene where you have a gesture recognizer and you set its delegate.

Then you dealloc this scene and its recognizer from its controller.

It would lead to crash if the delegate method was called in scene during this process.



回答4:

Actually this is not required. We are doing this to avoid forming retain cycle. We should not create a delegate with strong reference. If you accidentally created a delegate with strong reference then both parent and child will not get released. In that case dealloc itself will not get called. So it s not necessary.