开发者

Core Data Memory Leaks

开发者 https://www.devze.com 2023-03-14 17:32 出处:网络
I began testing my app in Instruments to clean up any memory leaks. I\'ve been able to clear up all the memory leaks except those related to Core Data. Instruments always points me to this section of

I began testing my app in Instruments to clean up any memory leaks. I've been able to clear up all the memory leaks except those related to Core Data. Instruments always points me to this section of code:

NSMutableArray *mutableFetchResults = [[managedObjectContext executeFetchRequest:request error:&error] mutableCopy];
if (mutableFetchResults == nil) {
    // Handle the error
}

I declare the managedObjectContext in my header file with the following code:

@interface UpperBody : UITableViewController <UITableViewDelegate, UITableViewDataSource> {

IBOutlet UITableView *upperTable;
NSMutableArray *exercises;
NSManagedObjectContext *managedObjectContext;

}

@property (nonatomic, retain) NSMutableArray *exercises;
@property (nonatomic, retain) NSManagedObjectContext *managedObjectContext;

I release the managedObjectContext in the (void)dealloc section. Here is the full section of code using the managedObjectContext:

- (void)loadExercises {

if (managedObjectContext == nil) {
    managedObjectContext = [(iFitAppDelegate *)[[UIApplication sharedApplication] delegate] managedObjectContext];
}

NSFetchRequest *request = [[NSFetchRequest alloc] init];
NSEntityDescription *entity = [NSEntityDescription entityForName:@"Exercises" inManagedObjectContext:managedObjectContext];
[requ开发者_开发知识库est setEntity:entity];

NSPredicate *predicate = [NSPredicate predicateWithFormat:@"category == 1"];
[request setPredicate:predicate];

NSSortDescriptor *sortDescriptor = [[NSSortDescriptor alloc] initWithKey:@"exerciseName" ascending:YES];
NSArray *sortDescriptors = [[NSArray alloc] initWithObjects:sortDescriptor, nil];
[request setSortDescriptors:sortDescriptors];
[sortDescriptor release];
[sortDescriptors release];

NSError *error = nil;
NSMutableArray *mutableFetchResults = [[managedObjectContext executeFetchRequest:request error:&error] mutableCopy];
if (mutableFetchResults == nil) {
    // Handle the error
}

[self setExercises:mutableFetchResults];
// [exercises addObject:@"Add Exercise"];
NSLog(@"Count of exercises %i", exercises.count);
[mutableFetchResults release];
[request release];
[self.tableView reloadData];

}

Any advise on what might be causing the leaks would be greatly appreciated! Thank you in advance!


kThere was probably a leak in the code I had above. I got around declaring an NSManagedObjectContext by just using a pointer to one whenever needed. Here is a sample of the code:

iFitAppDelegate *appDelegate = (iFitAppDelegate *)[[UIApplication sharedApplication] delegate];
NSManagedObjectContext *managedObjectContext = appDelegate.managedObjectContext;

This fixed my leak, so it must have had to do with how I was allocated and releasing the NSManagedObjectContext. Thank you for the pointers, @albertamg!

0

精彩评论

暂无评论...
验证码 换一张
取 消