Since upgrading to Lion and consequently to XCode 4.1
I get dozens of "potential memory leak" when running the Analyzer.
I would typically use a property list like so:
@synthesize indexPath = _indexPath;
- (id)initWithNibName:(NSString *)nibName bundle:(NSBundle *)nibBundle {
self = [super initWithNibName:nibName bundle:nibBundle];
self.indexPath = [[NSIndexPath alloc] init];
[_indexPath release];
return self;
}
and in dealloc() method:
- (void)dealloc {
[_indexPath release];
[super dealloc];
}
Now, analyze will show me the dreaded blue message on self.indexPath telling me that there's a leak. When there's obviously none.
How do you allocate and format your code so XCode doesn't believe it's leaking? (while keeping the property alias self.var vs _var)
Thank you...
The other answers already explain the problem in depth, anyway these are some common patterns that you can use to avoid this error:
NSIndexPath *ip = [[NSIndexPath alloc] init];
self.indexPath = ip;
/* ... */
[ip release];
indexPath = [[NSIndexPath alloc] init];
self.indexPath = [[[NSIndexPath alloc] init] autorelease];
self.indexPath = [NSIndexPath indexPathWithIndex:...];
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With