8

I know , the delegate is never retained! Ever!

But can anyone explain me why delegate is never retained ?...

Thanx in advance

4 Answers 4

14

It's a memory management thing.

Objective-C works with reference counts to keep the memory clean. This does mean that it can't detect cyclic relationships.

Example:

  • Object A owns object B. Object B is retained by object A.
  • Object B has a delegate that is object A. Object A is retained by object B.
  • Object C owns object A. Object A is retained by object C.
  • Object A now has a retainCount of 2, and object B has a retainCount of 1
  • Object C gets freed, and releases object A
  • Object A and B now have a retainCount of 1, because they own eachother. The system will not free them, because the retainCount is still 1 (still owned by another object)
  • Memory leak!
7
  • 2
    There is no garbage collector on iOS. Absolute retain counts are meaningless.
    – bbum
    Dec 20, 2012 at 15:57
  • @bbum There's no collector but there's collection. Retain counts are used internally by this system, but yes, you shouldn't ever use them in your own code. Dec 20, 2012 at 18:41
  • 1
    No, there isn't "collection", either. It is a pure passive reference counting system. "Collection" implies some kind of out-of-band processing related to memory management. The first three sentences don't make any sense when describing memory management on iOS, whether using manual memory management or ARC. The use of absolute retain counts in the answer isn't correct, either, as the absolute retain counts are meaningless.
    – bbum
    Dec 20, 2012 at 18:58
  • @bbum Reference counting is a form of garbage collection. And I disagree about the counts being meaningless: when they reach zero, the object is freed from the memory. While the developer of an application doesn't ever need to know the number, it's good to know how the reference counter works (retain increases the count, release decreases it, autorelease is a delayed decrease). Dec 20, 2012 at 21:45
  • 1
    @bbum I just did some reading on the matter and it would indeed appear that my terminology is wrong. I've updated my post. Dec 21, 2012 at 0:41
4

It's up to you. If you declare it to be retained (strong in ARC) it'll be retained.

The rule is to not retain it because it's already retained elsewhere and more important you'll avoid retain cycles.

2

To expand on djromero's answer:

If you have a UIViewController which contains a UITableView, the controller will be most likely retaining the table and it will be it's delegate / dataSource. If the table retains the delegate / dataSource, then they will be retaining each other and thus never getting released.

1

The whole idea is that the delegate should always outlive the object that assigns it as a delegate. So the webview will be released before the delegate that is assigned.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.