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.
This causes your leak:
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:
That returns an autoreleased array, so the eventual retain count will be only 1.