Cocoa Programming/Memory management

Someone, please fix this page

Dynamic allocation
In Objective-C (unlike in C++), all objects are created dynamically. This means that they continue to exist until they are explicitly destroyed.

To create an object, send its class an alloc message: [aClass alloc];. Note that this returns a new instance of the class but does not initialize its data. Typically you want to immediately send the new object an init message to remedy this. Allocation and initialization are usually combined in one line:

aClass *newobj = [[aClass alloc] init];

There is a class method called new which allocates and provides a bare-bones initialization of the object, but has been deprecated in favor of alloc and init.

Reference counts
In C++, every dynamically created object needs to be explicitly deleted (using the delete operator). In Java, every dynamically created object (that is, every object) will be automatically deleted when the garbage collector detects that the object is no longer referenced. In Objective-C neither of these approaches is used.

An Objective-C object keeps a reference count. When an object is created, its reference count is initialized to 1. When the reference count reaches zero, the object will self-destruct.

Release
To tell an object that you will no longer use it, send it a <tt>release</tt> message: "<tt>[anObject release];</tt>". This decrements the object's reference count by one. If you were the last user of the object, the object will destroy itself.

After sending <tt>release</tt> to an object it is not safe to use that object anymore since the object may have self-destroyed.

Retain
When you want to retain an object, i.e. prevent it from disappearing, send it a <tt>retain</tt> message: "<tt>[anObject retain];</tt>". Note that you should not send a retain message to an object you have created with an <tt>alloc</tt> message, because <tt>alloc</tt> always implies <tt>retain</tt>. Therefore, <tt>retain</tt> is only needed, if you need to use an object that is created elsewhere (and will be sent the <tt>release</tt> message from there when it's not needed in that part of the program anymore).

Equilibrium
Every <tt>retain</tt> needs to be matched by one and only one <tt>release</tt>. Note that the <tt>new</tt>, <tt>alloc</tt>, <tt>copy</tt>, and <tt>mutableCopy</tt> methods also implicitly <tt>retain</tt> the returned object, so those methods also need to be balanced with <tt>release</tt>.

Autorelease pool
Sometimes an object cannot be released immediately: for example, a function creates an object and wants to return it to the caller. Now, the callee should release the object it has created and won't be using anymore. However, the caller need to get access to the returned object. If the callee would <tt>release</tt> the created object, the caller would never see the return value. One solution is to skip releasing the object in the callee and demand that the caller will release it. This is error-prone and breaks the principle of "match every retain with one release".

In Objective-C/OpenStep runtime, it is possible to send an object an <tt>autorelease</tt> message. This will cause the object to be added to the current autorelease pool. Objects in an autorelease pool will be released at some point in the future (when the current autorelease pool is destroyed). They are safe to use prior to that.

If the caller wants to keep the returned object, it must <tt>retain</tt> it. Otherwise, the object will be automatically released. Example: