0

I have two UIViewControllers in a UINavigationController.
viewControllerA is the delegate of viewControllerB.
Obviously, viewControllerA doesn't have a reference to viewControllerB.
viewControllerB has a reference to viewControllerA because vcA is the delegate of vcB.

I've been told that delegates references should be weak.
In my case, I don't think it's true.
If the UINavigationController will decide to let go of vcA, vcB's delegate would be nil.
But, if the reference to vcA was strong, vcB would still have a reference to it.

Then why delegates are weak?
Yes, I know it's been asked before. Yes, I've spent time trying to understand this. Yet I don't seem to understand any of this.

Thank you.

Noam Solovechick
  • 1,127
  • 2
  • 15
  • 29
  • 1
    Because if an object X sets itself as the delegate of object Y, then very probably X has a strong ref to Y, and if Y had one to X (its delegate) too, that would cause a retain cycle. –  Jul 02 '13 at 18:31
  • I assume that vcA is the `rootViewController` and that vcB is pushed on top of that? – meaning-matters Jul 02 '13 at 18:31
  • @meaning-matters Exactly. – Noam Solovechick Jul 02 '13 at 18:41
  • 1
    Read [Is it ever Ok to have a 'strong' reference for a delegate?](http://stackoverflow.com/questions/17348523/is-it-ever-ok-to-have-a-strong-reference-for-a-delegate), a recent question on this topic which should explain the issue for you. – CRD Jul 02 '13 at 18:46
  • because the retain cycle can be broken at point to avoid memory leaks. – holex Jul 02 '13 at 18:57

2 Answers2

0

In my opinion, delegates should be weak (or assign in non-ARC code) to avoid cycles.

A typical case, that we see in many samples, is:

_myViewController = [[MyViewController alloc] init];
_myViewController.delegate = self;

This produces a cycle if the delegate is strong, because _myViewController is referenced by the original object, which in turn is referenced by _myViewController.

J_D
  • 3,526
  • 20
  • 31
  • You can remove `In my opinion` from your answer, that's the only reason why delegates should be `weak/assign`. ;)) – danypata Jul 02 '13 at 18:48
  • There can also be good reasons for a strong reference to a delegate, see `NSURLConnection`. – Martin R Jul 02 '13 at 18:50
  • @MartinR: Although they are both called "delegates," it seems to me that the temporarily retained object an NSURLConnection needs to get actual work done during its short lifetime is a somewhat different thing than, say, a view controller's delegate. – Chuck Jul 02 '13 at 18:57
  • @Chuck: Yes. I only said this because both the question and the answers quite generally state *"delegate references should be weak"*. – Martin R Jul 02 '13 at 19:00
0

I think when Class B is providing a delegate to Class A. Class A will assign a strong delegate to this delegate, now if Class B also makes this delegate Strong, this will result in Memory leak. As both class hold a strong reference to this delegate, it can't be released till some makes it weak and other one goes out of memory.

Good Link

Urmil Setia
  • 159
  • 1
  • 9