Hacker Newsnew | past | comments | ask | show | jobs | submitlogin

A. It uses reference counting. -retain (+1 now), -release (-1 now), -autorelease (-1 later).

B. Creation/allocation is +1, thus: +alloc (+1), -copy (+1).

C. Your goal is to keep the count balanced at 0. If you do a +1, make sure you do a -1 either now (-release) or later (-autorelease).

(C) is the guiding principle, thus in this order:

1. If you +alloc/-copy something in a function and is done with it, make sure you have a corresponding -release.

2. If you +alloc/-copy something in a function and return it, -autorelease before you return, so that counts are 0 (in the end anyway).

3. If you didn't +alloc/-copy an object, (1) and (2) don't apply. E.g. NSMutableString's +string.

4. If you -retain, make sure you -release or -autorelease to pair it up. (remember balanced to 0).

5. If you get an object from calling another function, you shouldn't -release/-autorelease it because someone else or you should have done (2). But don't forget (4).

The rest is about memory management in accessors and IB outlets.

PS: I give up on formatting this. Hope it's readable.



Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: