Re: Analyser reports memory leak… where?
Re: Analyser reports memory leak… where?
- Subject: Re: Analyser reports memory leak… where?
- From: Bill Cheeseman <email@hidden>
- Date: Thu, 12 Sep 2013 11:40:28 -0400
On Sep 12, 2013, at 11:11 AM, Graham Cox <email@hidden> wrote:
> On 12/09/2013, at 5:00 PM, Bill Cheeseman <email@hidden> 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 - email@hidden
_______________________________________________
Cocoa-dev mailing list (email@hidden)
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:
This email sent to email@hidden