Core Data Memory Leak

Discussion in 'iOS Programming' started by, Feb 18, 2012.

  1. macrumors regular

    Jun 26, 2011

    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.
    	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?
  2. macrumors regular

    Oct 18, 2007

    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
  3. macrumors 604

    Aug 9, 2009
    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 and Shop Towels On A Roll to your kit of Universal Engineering Tools.

Share This Page