I have two table controllers showing CoreData objects. One is a detailed view (with sentences), one of them is a review (with stories). Select a story → See Suggestions.
It looks like I re-released my managed ObjectContext; I originally released it both in TableViewControllers in dealloc, and the third time I went between two controllers (Story → Sentence → Story → Sentence → Story → Crash). Some debugging showed that I crash in my delegate delet after this code in the ViewDidLoad of both TableViewControllers:
if (managedObjectContext == nil)
{
managedObjectContext = [(StoryBotAppDelegate *)[[UIApplication sharedApplication] delegate] managedObjectContext];
NSLog(@"After managedObjectContext: %@", managedObjectContext);
}
A few more studies found this discussion , which made me believe that this was the case with the released ManagedObjectContext:
The second more prosaic issue is the simply overridden NSManagedObject. Tools The ObjectAlloc tool should be able to help you.
So, I deleted [managedObjectContext release]; from my dealloc to TableViewController, and now I have no leaks (according to the tools) and without failures.
It seems the problem is fixed, but here is the question:
Perhaps I don’t have a point at all, and I'm just hiding another problem. How can I find a redundant release or a real problem?
If I fixed the problem, I would like to know why it is fixed and why I do not need to free MOC in the second TableViewController
MakeSentenceTableViewController.m
@implementation MakeSentenceTableViewController
@synthesize story, managedObjectContext;
- (void)viewDidLoad {
[super viewDidLoad];
self.title = @"My Story";
NSLog(@"Passed Story Object: %@", story);
if (managedObjectContext == nil)
{
NSLog(@"managedObjectContext == nil");
managedObjectContext = [(StoryBotAppDelegate *)[[UIApplication sharedApplication] delegate] managedObjectContext];
NSLog(@"After managedObjectContext: %@", managedObjectContext);
}else{
NSLog(@"managedObjectContext != nil");
}
NSFetchRequest *request = [[NSFetchRequest alloc] init];
NSEntityDescription *entity = [NSEntityDescription entityForName:@"Sentence" inManagedObjectContext:managedObjectContext];
[request setEntity:entity];
NSSortDescriptor *sortDescriptor = [[NSSortDescriptor alloc] initWithKey:@"order" ascending: YES];
NSArray *sortDescriptors = [[NSArray alloc] initWithObjects: sortDescriptor, nil];
[request setSortDescriptors:sortDescriptors];
[sortDescriptors release];
[sortDescriptor release];
fetchedResultsController = [[NSFetchedResultsController alloc]
initWithFetchRequest:request managedObjectContext:managedObjectContext
sectionNameKeyPath:nil cacheName:nil];
[request release];
NSError *error;
[fetchedResultsController performFetch:&error];
NSLog(@"FetchedResultsController: %@", fetchedResultsController);
NSLog(@"fetchedResultsController RetainCount at viewDidLoad: %d", [fetchedResultsController retainCount]);
}
- (void)dealloc {
[fetchedResultsController release];
[super dealloc];
}
source
share