autorelease

The reference-counted memory management system

Cocoa use a counter, the retainCount to know when an object must be deallocated.

When an object is initialized, its retainCount value is set to one, meaning "I am used at one place".

During it's life, this object may encounter several call to retain or release.

When retain is called, the retainCount value is incremented, meaning "I am used at one more place". Inversely, when release is called, the retainCount value is decremented. When the counter value reaches zero, the object is deallocated.

The autorelease method is a variant to the simple release one. When an object receive this message, it adds itself to the nearest autoreleasepool. When this pool is released or train, all objects that were put in it receive a release message.

You can create your own autoreleasepool, sometimes you must do it. There exist at least one pool in your program, located in the event loop.

Returning an object from a method

It's quite common for a method to return an object, but there are several things you should consider.
First of all, are you returning an already existing object or are you creating one?
If you are creating one, do you need to keep it?
If the object was already existing, you can simply just return it in most cases.
If it is created in the method, there are two possibilities.

  • The created object won't be used anymore after that

  • You want to keep the created object for later use.

In the first case, your newly created object should receive an autorelease instruction before being returned.
In the second case, we are probably in a case of lazy allocation. It's a name given for the initialization of a class attribute outside an init method.

- (id)myMethod {
    if (!m_myAttribute)
    {  
/* If the object wasn't allocated yet */
        
@synchronized(self)
        {  
/* To protect this part from multiple access */
            
if (!m_myAttribute)
            {
/* If the object is still not allocated (second part of the security against multiple access) */
                
m_myAttribute = [[MyClass alloc] init];
            }
        }
    }
    
return m_myAttribute;
}


The first time this method is called, the object will be allocated and initialized before being returned. The second time the already allocated object will only be returned. So, as a matter of fact, we mustn't autorelease this object because it will be reused.