Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9202299
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T23:13:25+00:00 2026-06-17T23:13:25+00:00

I am trying to debug a memory leak within my code (2.5mb) and it

  • 0

I am trying to debug a memory leak within my code (2.5mb) and it seems to be pointing to _prepareResultsFromResultSet within NSFetchedResultsController. I have 2 viewControllers (A & B), viewController B is pushed on to the navigationController from viewController A.

In B I am performing an NSFetchRequest using NSFetchedResultsController as follows:



.h

@property (nonatomic, retain) NSFetchedResultsController *fetchedResultsController;
@property (nonatomic, retain) NSString *sMapSlug;
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil andMapSlug: (NSString *)slug;

.m

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil andMapSlug: (NSString *)slug
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
        self.sMapSlug = [NSString stringWithString:slug];
    }
    return self;
}
- (NSFetchedResultsController *)fetchedResultsController {

    if (_fetchedResultsController != nil)
        return _fetchedResultsController;

    Singleton *singleton = [Singleton sharedSingleton];

    NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];
    NSEntityDescription *entity = [NSEntityDescription entityForName:@"DescriptionEntity" inManagedObjectContext:[singleton managedObjectContext]];
    [fetchRequest setEntity:entity];

    NSSortDescriptor *sort = [[NSSortDescriptor alloc] initWithKey:@"map.sName" ascending:NO];
    [fetchRequest setSortDescriptors:[NSArray arrayWithObject:sort]];
    NSPredicate *myPredicate = [NSPredicate predicateWithFormat:@"map.sSlug LIKE %@", self.sMapSlug];
    [fetchRequest setFetchBatchSize:8];
    [fetchRequest setPredicate:myPredicate];

    // Finally check the results
    NSError *error;
    NSArray *fetchedObjects = [[singleton managedObjectContext] executeFetchRequest:fetchRequest error:&error];
    for (DescriptionEntity *desc in fetchedObjects)
    {
        NSLog(@"Maps present in database: %@", desc.map.sName);
    }        

    NSFetchedResultsController *theFetchedResultsController = [[NSFetchedResultsController alloc] initWithFetchRequest:fetchRequest managedObjectContext:[singleton managedObjectContext] sectionNameKeyPath:nil cacheName:@"Test1"];
    self.fetchedResultsController = theFetchedResultsController;
    self.fetchedResultsController.delegate = self;
    //[self.fetchedResultsController performFetch:NULL];
    [theFetchedResultsController release];
    [fetchRequest release];
    [sort release];

    return _fetchedResultsController;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    [self.fetchedResultsController fetchRequest];
}

Everything is loaded in fine using the fetchRequest and I can access my data if need be, however I have taken this out for now so I know none of the data from the NSFetchedRequestController is being used. When I go back on the navigation stack the dealloc for controller B is called, and I perform the following:

- (void)dealloc {
    self.fetchedResultsController.delegate = nil;
    [_fetchedResultsController release];
    [sMapSlug release];

    [super dealloc];
}

When I take heap shots of this within Instruments I see the following data stay after the dealloc is called:

enter image description here

If I go back in to controller B again, another set of this memory is added and never removed. However, if I do this a 3rd time the amount does not increment. I am presuming that there is some sort of caching going on, can anyone tell me how to remove this data or how to correctly deallocate an NSFetchedResultsController.

If you need any more information feel free to ask.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-17T23:13:26+00:00Added an answer on June 17, 2026 at 11:13 pm

    Usually you should not worry about memory management in Core Data. Yes, under the hood a caching mechanism is involved.

    Anyway, there are two different methods to “dismiss” the memory.

    The first is refreshObject:mergeChanges: method. Passing to it NO, it allows to prune the object graph. In other words, it throws away any changed data that has not been saved to the store.

    Override willTurnIntoFault and didTurnIntoFault to see it in action.

    The other is to call reset on a managed context. Obviosly, this removes all the objects the context contains.

    Hope that helps.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I've been trying to debug a memory leak in my program, and have narrowed
I'm trying to debug a memory leak in a Node.js application but cannot install
I'm trying to debug a memory leak in a printer driver. I'm pretty sure
I'm trying to debug kernel module. I suspect to have there some memory leaks.
I have a memory leak that I'm trying to hunt down in my mfc
Trying to debug a memory leak in a java program (running on linux 64bits),
I'm trying to debug a memory leak (see question Memory leak in Python Twisted:
I’m trying to debug a memory leak problem. I’m using mtrace() to get a
I am having real problems trying debug my code to malfunctioning print statements. I
I am trying to debug memory usage in a large application using Delphi 7.

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.