After reading the Memory Management Guide I am trying to get my head around what is happening here My understanding “so far” is …
- (EngineClass *)engine {
return [[engine retain] autorelease];
}
- I am returning a pointer to an Engine object.
- The retain count of the object is incremented to record we are making a new pointer to the object
- Balance the above retain by specifying an autorelease at some future point.
Am I on the right track?
cheers -gary-
I’m not sure I’d say the retain is “to record we are making a new pointer to the object.”
The retain-autorelease pair is there to indicate that there’s another object interested in the return value, so it needs to stick around at least that long, but the other object doesn’t properly own the return value. Without the retain, if the object performing this method is deallocated immediately afterward, the engine might not have any other owners and would also be deallocated immediately. That’s not the expected behavior. Doing it this way causes the object’s ownership not to go away until the autorelease pool is drained.