On 12/09/2013, at 1:59 PM, Bill Cheeseman <wjcheese...@gmail.com> wrote:

> In my understanding, Analyze is meant to be very literal, even simple-minded. 
> Your method returns a retained object but you did not put "new" or "copy" in 
> the method name. It assumes that you were following the naming convention and 
> that your omission of "new" or "copy" was intentional. It therefore reported 
> to you that the return of a retained object is an error. It is telling you 
> that your code is wrong only in this very literal and simple-minded sense.


I do see this, but what I'm not quite getting is why this doesn't get reported 
for all sorts of other property getters that also correctly return retained 
objects (ivars, typically) that are not owned by the caller.

> In fact, your comment in the last method, which does have "new" in its name, 
> demonstrates that you are aware of the naming convention. Why did you not 
> follow the convention in the other method? Add "new" or "copy" to the name 
> and build it again. The Analyze error went away, right?


I didn't write this code, I wouldn't have done it this way - I generally avoid 
writing 'newXXX' methods to return objects that the caller has to release if I 
can help it. For some reason the author of this code did it this way.

In this case, -eventTypes is just a normal property getter which initializes 
the ivar lazily, using an internal 'new' method to do it. What's not clear to 
me is why it cares that the temporary variable, which is dealt with correctly, 
has a retain count of 1, having assigned to the property's ivar. How is that 
different to returning a retained property ivar at any other time? Changing the 
getter's name to include 'new' or 'copy' might shut up the analyser, but it's 
incorrect since a) the caller should NOT release the return value (that would 
leave the ivar dangling) and b) it breaks the convention for property getter 
names.


-----

I've now slightly modified the code to do things a little more conventionally, 
refactoring the name of 'newEventTypes' to 'makeEventTypes' and returning an 
autoreleased object, which my -eventTypes getter no longer releases. The 
analyser is now happy, even though it is *still* returning the ivar with a +1 
retain count and nothing else has changed.

I guess it just isn't expecting code that follows a pattern that, while 
apparently legal, is not really commonly used.


--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

Reply via email to