Re: static analyzers says I'm leaking, I _think_ I'm not
Re: static analyzers says I'm leaking, I _think_ I'm not
- Subject: Re: static analyzers says I'm leaking, I _think_ I'm not
- From: Michael David Crawford <email@hidden>
- Date: Wed, 06 May 2015 14:28:44 -0700
John McCall wrote:
> You should probably make the property readonly and just directly assign to the ivar in -init.
That works.
In this particular case, it might make more sense for my property to
be just a regular ivar, without a getter and setter. That is, my grid
object owns a cycler object, client code turns the cycler on and off,
adjusts its speed and so on. My code might be easier to maintain if
the grid exposed an interface to do all that.
Michael David Crawford, Consulting Software Engineer
email@hidden
http://www.warplife.com/mdc/
Available for Software Development in the Portland, Oregon Metropolitan
Area.
On Wed, May 6, 2015 at 2:21 PM, John McCall <email@hidden> wrote:
>> On May 6, 2015, at 1:57 PM, Michael David Crawford <email@hidden> wrote:
>> I've had problems in the past where I failed to understand the Cocoa
>> ownership conventions, I'm willing to grant that could be the case.
>>
>> I know for sure that the analyzer enforces the naming conventions,
>> that is, the exact same function names in C or C++ won't yield that
>> same warnings as "alloc", "new" or "copy" would in Objective-C.
>>
>> I'm reluctant to use ARC because in my honest opinion, ARC will get
>> the leaks and most out of the crashes out of skanky code. If you
>> don't use ARC, you get them out by fixing the skank:
>>
>> "Dave, why do you add twelve extra bytes to all of Spellswell's allocations?"
>>
>> "That was so Spellswell would stop crashing." Facepalm.
>>
>> // LifeGrid.h
>> @property (assign, nonatomic) GridCycler *cycler;
>>
>> // Lifegrid.m - init
>> self.cycler = [[GridCycler alloc] initWithGrid: self]; // Potential
>> leak of an object
>> if ( nil == self.cycler ) goto cycler_failed;
>>
>> // dealloc
>> [self.cycler release];
>>
>> Expanding the "potential leak" message yields:
>>
>> 1. assuming 'self' is not nil
>>
>> 2. method returns Objective-C object with +1 retain count
>>
>> 3. Object leaked: allocated object is not references later in this
>> execution path and has a retain count of +1.
>>
>> Isn't that what I want? I should be taking ownership of it with
>> "alloc/initWithGrid".
>>
>> (initWithGrid doesn't do a cyclic retain.)
>
> "cycler" is really a strong property (because you release its value in -dealloc), but you've declared it as assign, which means the synthesized setter won't retain the new value or release the old value. One way of looking at this is that the setter takes its argument at +1 and has a precondition that the property is currently set to nil. Another way of looking at it is that the setter is buggy.
>
> You should probably make the property readonly and just directly assign to the ivar in -init.
>
> John.
_______________________________________________
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