On 12/09/2013, at 5:40 PM, Bill Cheeseman <wjcheese...@gmail.com> wrote:
> But -eventTypes is not in fact a valid getter because it returns the iVar > retained but not autoreleased. How is it not valid? A simple version of it would be: - (id) eventTypes { return mEventTypes; } which is perfectly valid. The rest of the code is just a lazy initialization for mEventTypes. > The setter/getter convention requires getters to return autoreleased objects. Not so, it requires that the returned object is not owned by the caller. That is all. > So leaving it named according to the getter naming convention is wrong. > Adding "new" would not only be correct but would also warn against using it > as a getter. But it is a getter, no more or less. The lazy initialization is neither here nor there to the caller. > As written (no matter what it is named), it is in fact the caller's job to > release it. That is presumably why you release it in -dealloc. If your > class's -init method initially created the first object assigned to the iVar, > whether by calling -eventTypes or by directly calling -newEventTypes and > setting the iVar, then you are the owner and you must release it in -dealloc > if not earlier. And if you call -eventTypes or -newEventTypes while > substituting a new object for the old iVar object in midstream, you must > release the old iVar object in midstream, too. The object owns mEventTypes, so it is not the caller's job to release it. It is released in -dealloc because the object owns mEventTypes. > At it's core, that's what is really "odd" about the code. It uses a getter > that does not comply with the getter convention. I believe it does. I think your reading of the getter convention may be incorrect. If you can point to explicit documentation that states that the returned object must belong to an autorelease pool, I'll stand corrected, but that would be the first time I've ever heard that in 13 years of Cocoa programming! --Graham _______________________________________________ Cocoa-dev mailing list (Cocoa-dev@lists.apple.com) Please do not post admin requests or moderator comments to the list. Contact the moderators at cocoa-dev-admins(at)lists.apple.com Help/Unsubscribe/Update your Subscription: https://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com This email sent to arch...@mail-archive.com