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 (Cocoa-dev@lists.apple.com)

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 arch...@mail-archive.com

Reply via email to