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: Daniel Höpfl <email@hidden>
- Date: Thu, 07 May 2015 13:44:48 +0200
Hi,
Expanding the "potential leak" message yields:
Static analyser is right, there is a potential leak.
Based on what Aaron said, assume the following usage of your class:
LifeGrid *grid = [[LifeGrid alloc] init];
// GridCycler is allocated (retain count: +1)
// GridCycler is retained by property assignment (retain count: +2)
grid.cycler = nil;
// GridCycler property retainment is released (retain count: +1)
// No new object to retain.
[grid release];
// property is nil, your release call is ignored -> leak.
Even worse, resulting in crash (double dealloc) on/after autorelease
drain:
LifeGrid *grid = [[LifeGrid alloc] init]; // A: +2
grid.cycler = [[[GridCycler alloc] initWithGrid:grid] autorelease]; //
A: +1, B: +2, AR pending.
[grid release]; // cyclerA: +1, cyclerB: 0, but still 1 autorelease
pending (!)
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.
Bye,
Daniel
_______________________________________________
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