On 07.05.2015 20:16, Charles Srstka wrote:
> On May 7, 2015, at 6:44 AM, Daniel Höpfl <[email protected]
> <mailto:[email protected]>> wrote:
>>
>> I'd change the code as follows:
>>
>> // init
>> self.cycler = [[[GridCycler alloc] initWithGrid: self] autorelease];
>>
>> // alternative init, if you want to bypass the autorelease pool:
>>
>> GridCycler *cycler = [[GridCycler alloc] initWithGrid: self];
>> self.cycler = cycler;
>> [cycler release];
>> // Don't use [self.cycler release], one day you might change the
>> property to copy.
>>
>>
>> // dealloc:
>> // nothing.
> 
> The dealloc is wrong here; that still has to release the property,
> either by setting it to nil or by releasing the ivar (not through a
> property access). With MRC, properties aren’t automatically released on
> dealloc.
> 
> So, in dealloc, either:
> 
> [_cycler release];
> 
> or:
> 
> self.cycler = nil;

You are right: I was wrong.

cycler has to be released in dealloc, but using the setter (self.cycler)
does not help here: The property is marked as "assign".

So, to fix the code:

a)
@property (assign, nonatomic, readonly) GridCycler *cycler;

init:
_cycler = [[GridCycler alloc] initWithGrid: self];

dealloc:
[_cycler release];

b)
@property (strong, nonatomic) GridCycler *cycler;

init:
_cycler = [[GridCycler alloc] initWithGrid: self];

dealloc:
[_cycler release];

or c)
@property (strong, nonatomic) GridCycler *cycler;

init:
GridCycler *cycler = [[GridCycler alloc] initWithGrid: self];
self.cycler = cycler;
[cycler release];

dealloc:
[_cycler release];

(or d: Like a) but use a ivar.)

ARC has its pitfalls but simple things are simpler than in MRC ...

Bye,
   Daniel
_______________________________________________

Cocoa-dev mailing list ([email protected])

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 [email protected]

Reply via email to