views:

297

answers:

3

I am having a specific leak that I can't seem to dig to the bottom of, cause my search always ends up in some Apple libraries. Any help from some veterans at dealing with this would be appreciated.

Here is the relevant source code: (leak indicated with a comment)

- (void)viewDidLoad {
//[super viewDidLoad];

NSManagedObjectContext *context = [(iEatAppDelegate*)[[UIApplication sharedApplication] delegate] managedObjectContext];

addButton = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemAdd target:creator action:@selector(launchCreatorWindow)];
self.navigationItem.rightBarButtonItem = addButton;

NSFetchRequest *fetReq = [[NSFetchRequest alloc] init];
[fetReq setEntity:[NSEntityDescription entityForName:entityName inManagedObjectContext:context]];
NSError *e = nil;

NSArray *temp = [context executeFetchRequest:fetReq error:&e];  
//leaking here according to performance tool

self.tableObjects = [[NSMutableArray alloc] initWithArray:temp];

if (e) {
    NSLog(@"%@, %@", e, [e description]);
}
[fetReq release];
}

I tried releasing temp, but it crashed with the EXC_BAD_ACCESS which i believe means it is autoreleased already.

The leaks performance tool says it is a Category: CFArray (store-deque) Event: Malloc also says my library is responsible. This is the stack trace, number 8 being my viewDidLoad frame:

0 CoreFoundation __CFAllocatorSystemAllocate
1 CoreFoundation CFAllocatorAllocate
2 CoreFoundation _CFAllocatorAllocateGC
3 CoreFoundation _CFArrayReplaceValues
4 CoreFoundation CFArrayReplaceValues
5 CoreFoundation -[__NSPlaceholderArray initWithObjects:count:]
6 CoreFoundation -[NSArray initWithArray:copyItems:]
7 CoreFoundation -[NSArray initWithArray:]

This one really has me stuck, any help is greatly appreciated.

A: 

The answer is I am missing something basic. [NSMutableArray alloc] retain count = 1 self.tableObjects = retain count = 2 dealloc retain count still not 0 leak.

Sorry about this

Alex Gosselin
A: 

This isn't the cause of your leak but I thought it'd be worth pointing out anyway: You should release addbutton right after you have assigned it to the rightBarButtonItem seeing as it is not going to be needed/used again:

self.navigationItem.rightBarButtonItem = addButton;
[addButton release];
EddieCatflap
+1  A: 

This causes your leak:

self.tableObjects = [[NSMutableArray alloc] initWithArray:temp];

You create an array with a retain count of 1, then you use self.tableObjects which (if that property is marked as retain) brings the count up to 2.

Then in dealloc when you release the array the count is back down to 1, not 0 - so the array is never released.

Instead, just do this:

 self.tableObjects = [NSMutableArray arrayWithArray:temp];

That returns an autoreleased array, so the eventual retain count will be only 1.

Kendall Helmstetter Gelner