On Sep 12, 2013, at 11:11 AM, Graham Cox <graham....@bigpond.com> wrote:

> On 12/09/2013, at 5:00 PM, Bill Cheeseman <wjcheese...@gmail.com> wrote:
> 
>> Fix the name and the Analyzer is happy. No need to change the code, because 
>> the code is correct
> 
> Except that 'fixing' the name would make the code incorrect, because then it 
> isn't a valid getter name.
> 
> If you did go ahead and name it thus, the caller would assume that it's their 
> job to release the returned value. If they did that, it would leave 
> mEventTypes dangling, and a later call to -release in -dealloc would crash 
> due to an overrelease. So renaming the method is wrong on both counts.


But -eventTypes is not in fact a valid getter because it returns the iVar 
retained but not autoreleased. The setter/getter convention requires getters to 
return autoreleased objects. 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. 

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.

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.

-- 

Bill Cheeseman - b...@cheeseman.name

_______________________________________________

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

Reply via email to