views:

39

answers:

1

I am getting a memory leak when I add a sort descriptor to my Fetch Request in Core Data. If I comment out the NSSortDescriptor block it runs without a memory leak in Instruments.

NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];

NSEntityDescription *entity = [NSEntityDescription entityForName:@"Pools" inManagedObjectContext:self.managedObjectContext];
[fetchRequest setEntity:entity];

NSPredicate *predicate = [NSPredicate predicateWithFormat:@"game.league.id=%i",[lid intValue]];
[fetchRequest setPredicate:predicate];

NSSortDescriptor *sortDescriptor = [[NSSortDescriptor alloc] initWithKey:@"game.date" ascending:YES];
NSArray *sortDescriptors = [[NSArray alloc] initWithObjects:sortDescriptor, nil];
[fetchRequest setSortDescriptors:sortDescriptors];
[sortDescriptors release];
[sortDescriptor release];

NSError *error;
NSArray *items = [managedObjectContext executeFetchRequest:fetchRequest error:&error];

// Do Something

[fetchRequest release];
[items release];

Any ideas?

+1  A: 

In the sort descriptor, you're walking a relationship using the keypath game.date.

Your leak is probably associated with one of those objects in the keypath. It disappears when you remove the sort because then the objects in the key path don't do anything.

If you have transient properties, custom accessors, non-entity properties etc in the game entity I would look there. Custom value transformers are also a good bet.

The stack in Instrument should tell you exactly which object is leaking.

TechZen
There isn't any custom code in there. Could the core data stack just be setup wrong?
sublet
Unlikely. Why would the leak show up here instead of where you setup the Core Data stack? Instruments will tell you what is leaking. Drag this source file into Instruments and it will show you the leaking line. Examine the stack for that line and it will show you which object leaks. It's probably in the `//do something` code related to the `game.date`.
TechZen