Retain count rules clarifications


Under the section “Retain Count rules” the first rule states:

[quote]If you create an object using a method whose name starts with alloc or new or contains copy, then you have taken ownership of it. …
You have a responsibility to release the object when you no longer need it.

Does this mean that every object I create with “alloc / init” I need to set it to “nil” to release it properly?

For example, in the following chapter we create the “executives” object below. Is it true that I need to manage its memory allocation because I created it with “alloc / init”?

    NSMutableDictionary *executives = [[NSMutableDictionary alloc] init];

…and then I need to add this somewhere in the code…

   executives = nil;


From the best of my understanding…

If you’re using ARC with Xcode 4.2 (and up), you don’t need to do anything with retain.

If you’re managing memory manually, you need to send your retained object a release message when you’re through with it. This may result in it being removed, but other objects may still have retained it, delaying its demise.

Setting the pointer to nil offers you some level of assurance that you won’t accidentally try to send a message to an object that no longer exists, but is otherwise inconsequential.