Lazy allocation

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.