Re: Analyser reports memory leak… where?
Re: Analyser reports memory leak… where?
- Subject: Re: Analyser reports memory leak… where?
- From: Kyle Sluder <email@hidden>
- Date: Thu, 12 Sep 2013 09:07:12 -0700
On Thu, Sep 12, 2013, at 02:35 AM, Graham Cox wrote:
> Here's some code for which the Analyser reports "potential leak of an
> object stored into 'eventTypes'". I don't see it.
>
> I didn't write this code, so I'm reluctant to change it even though I
> would have written it a bit differently. mEventTypes is an ivar.
>
> - (void)setEventTypes:(NSDictionary*)eventTypes
> {
> if (eventTypes != mEventTypes)
> {
> [mEventTypes release];
> mEventTypes = [eventTypes retain];
> }
> InitializePrefsForEventTypeNames();
> }
>
> - (NSDictionary*)eventTypes
> {
> if (mEventTypes == nil)
> {
> [self loadNib];
>
> NSDictionary* eventTypes = [self newEventTypes];
> [self setEventTypes:eventTypes];
> [eventTypes release];
> }
>
> return mEventTypes; //<----- analyser complains here
> }
>
>
>
> - (NSDictionary*)newEventTypes
> {
> //[code deleted that presets contents of 'eventTypes']
>
> // Method name begins with "new"; clients are responsible for releasing.
> return [[NSDictionary alloc] initWithDictionary:eventTypes];
> }
Looks like an analyzer bug. Your code is correct, but I bet the call to
-setEventTypes from within -eventTypes is throwing it off.
Personally, I would avoid doing this, as it could cause reentrancy among
KVO observers of eventTypes. Instead, I'd assign to mEventTypes directly
from your lazy initializer. Or perhaps I would factor out the common
setter code to a private helper method.
Hopefully this has the side effect of shutting the analyzer up.
--Kyle Sluder
_______________________________________________
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