PDA

View Full Version : Core Data Memory Leak




ahan.tm
Feb 18, 2012, 10:07 AM
Hi,

I am fetching data from core data and putting it into an array using the following code:
/*
Fetch existing events.
Create a fetch request; find the Event entity and assign it to the request; add a sort descriptor; then execute the fetch.
*/
marblebeingdragged=YES;
NSFetchRequest *request = [[NSFetchRequest alloc] init];
NSEntityDescription *entity = [NSEntityDescription entityForName:@"Child" inManagedObjectContext:_managedObjectContext];
[request setEntity:entity];

// Order the events by creation date, most recent first.
NSSortDescriptor *nameDescriptor = [[NSSortDescriptor alloc] initWithKey:@"name" ascending:NO];
NSSortDescriptor *prizeDescriptor = [[NSSortDescriptor alloc] initWithKey:@"prize" ascending:NO];
NSSortDescriptor *neededDescriptor = [[NSSortDescriptor alloc] initWithKey:@"marblesneeded" ascending:NO];
NSSortDescriptor *colorDescriptor = [[NSSortDescriptor alloc] initWithKey:@"color" ascending:NO];
NSSortDescriptor *reachedDiscriptor = [[NSSortDescriptor alloc] initWithKey:@"prizereached" ascending:NO];

NSArray *sortDescriptors = [[NSArray alloc] initWithObjects:nameDescriptor,prizeDescriptor,neededDescriptor,colorDescriptor, nil];
[request setSortDescriptors:sortDescriptors];
[nameDescriptor release];
[colorDescriptor release];
[prizeDescriptor release];
[neededDescriptor release];
[reachedDiscriptor release];
[sortDescriptors release];

// Execute the fetch -- create a copy of the result.
NSError *error = nil;
records = [[self.managedObjectContext executeFetchRequest:request error:&error] retain];

[request release];


However, when using this code however, Instruments shows memory leaks associated with
records = [[self.managedObjectContext executeFetchRequest:request error:&error] retain];

If I release records after I release request, my app crashes as records is called throughout my view controller. When and where should I release records? Should I use autorelease? If so, how?



RonC
Feb 20, 2012, 11:49 AM
Nothing seems obviously wrong to me. Does the warning go away if you enable ARC? If not, then there is something more fundamental at play; if so, then it's fixed. :)

Reminds me a little bit of a diagram on my colleague's wall:
Universal Engineering Checklist - Answer these 2 questions: Does it move? Should it?

Yes and yes: do nothing
No and no: do nothing
Yes and no: duct tape
No and Yes: WD-40

chown33
Feb 20, 2012, 11:59 AM
Universal Engineering Checklist - Answer these 2 questions: Does it move? Should it?

Yes and yes: do nothing
No and no: do nothing
Yes and no: duct tape
No and Yes: WD-40

And if you erroneously answered Yes and No, but then realized it should be No and Yes, then WD-40 acts as a solvent for the duct tape adhesive.

If the error is in the other direction, then add Everclear (http://en.wikipedia.org/wiki/Everclear_(alcohol)) and Shop Towels On A Roll to your kit of Universal Engineering Tools.