In your second array example, after the object ownership has been transferred, you would use the autorelease in the dealloc method there, right? or have i got it wrong?
You don't have to release or autorelease the object in that case. It will get released implicitly once the array is emptied (in case of NSMutableArrays) or released. In other words: Releasing that object is the array's problem, not yours.
BTW, I don't think it's helpful to think of these things as "ownership", which can be "transferred". Any given object can have lots of different owners at the same time. Also, object A may retain object B more than once.
But personally, I like real-world metaphors, too. Here's what I do: I think of retaining as a kind of popularity contest. Objects get dealloc'ed when no other object likes them anymore, i.e., when all of the objects that once alloc'ed and/or retained them have released them fully.
You wouldnt happen to have a sample of this would you?
Here's the basic concept:
Code:
+ (SomeObject*)someObjectConvenientlyReturnedAsAutoReleased {
return [[[SomeObject alloc] init] autorelease];
}
However, you'll usually do this within SomeObject's implementation, so you'd rewrite it as:
Code:
+ (id)someObjectConvenientlyReturnedAsAutoReleased {
return [[[self alloc] init] autorelease];
}
Doing it this way comes with two subclass-related advantages:
1. If you're referring to the class as "self" instead of hard-coding it as "SomeObject" when allocating, subclasses of SomeObject (e.g., SomeObjectDerivative) will work as expected and return objects of the subclass type.
2. By returning a result of type "id" (read: "any NSObject or NSObject subclass") instead of "SomeObject", you avoid the compiler warning you'd otherwise get for doing something along the lines of ...
Code:
SomeObjectDerivative *object = [SomeObjectDerivative someObjectConvenientlyReturnedAsAutoReleased];
... with SomeObjectDerivative being a SomeObject subclass, as mentioned above. Otherwise, you'd have to do an explicit typecast, which I, personally (again

), consider ugly.