I am new to iphone development and I was struggling with the EXC_BAD_ACCESS error that I received a couple of days ago. I mainly do the Stanford iphone class myself, and I'm trying to pass an array of NSManagedObjects to a TableViewController that should display them. The application runs in the simulator and displays the data in the View table, but immediately eliminates errors using EXC_BAD_ACCESS.
I have followed instructions here and elsewhere on how to use NSZombieEnabled to identify prematurely released objects, but this code does not contain any useful messages even with NSZombieEnabled. I guess this should be caused by something trying to access unassigned memory that was not released through release / autorelease. Or it would be perceived as a zombie object, like other errors that I could fix. I am not an expert c, but does this mean that this can happen if I have to declare an object and send it a message without creating it? I looked through my code to find out if I have something like this, and I went up to it.
I have a stack trace in the debugger for this, but I'm not sure how to use it. I'm a little upset because I can't use breakpoints in the code to narrow down the problem further, since it seems to happen after the application finishes loading. I thought that the application would simply remain idle if there was no user interaction. Is this a failure in the tail of the download, where I cannot easily see it, or do something in the background after loading. And I would really appreciate any advice on how to read stacktrace for this.
I printed my glass below (I could not figure out how to copy it from the debugger)
0 objc_msgSend
1 ??
2 -[NSManagedObject dealloc]
3 -[_PFManagedObjectReferenceQueue _processReferenceQue:]
4 _performRunLoopAction
5 ___CFRunLoopDoObservers
6 CFRunLoopRunSpecific
7 CFRunLoopRunInMode
8 GSEventRunModal
9 GSEventRun
10 UIApplicationMain
11 main
And the two main classes in my program are the top-level delegate class and the ViewTableController it calls.
`- (void) applicationDidFinishLaunching: (UIApplication * application) {
self.tabBarController = [[[UITabBarController alloc] init] autorelease];
UINavigationController *contactsNavigationController = [[self createContactsNavigationController] retain];
tabBarController.viewControllers = [[NSArray alloc] initWithObjects: contactsNavigationController, nil];
[contactsNavigationController release];
[window addSubview:tabBarController.view];
[window makeKeyAndVisible];
}
- (UINavigationController *) createContactsNavigationController {
UINavigationController *contactsNavigationController = [[UINavigationController alloc] init];
UITabBarItem *contactsTabBarItem = [[UITabBarItem alloc] initWithTabBarSystemItem: UITabBarSystemItemContacts tag:0];
contactsNavigationController.tabBarItem=contactsTabBarItem ;
[contactsTabBarItem release];
PersonListViewController *personListViewController = [[PersonListViewController alloc] initWithStyle:UITableViewStylePlain];
NSManagedObjectContext *context = [self managedObjectContext];
personListViewController.managedObjectContext=context;
personListViewController.contacts = [self createContacts];
[context release];
personListViewController.title=@"Contacts";
[contactsNavigationController pushViewController:personListViewController animated:false];
return [contactsNavigationController autorelease];
} `
`- (NSArray *) readContacts {
NSString *path = [[NSBundle mainBundle] bundlePath];
NSString *filePath = [path stringByAppendingPathComponent:@"FakeData.plist"];
NSArray *plist = [[NSMutableArray arrayWithContentsOfFile:filePath] retain];
return [plist autorelease];
}
- (NSMutableArray *)createContacts {
NSArray * plist = [[self readContacts] retain
NSMutableArray *contactNames = [[NSMutableArray alloc] init];
NSMutableArray *contacts = [[NSMutableArray alloc] init];
for (NSDictionary *photo in plist) {
NSString *contactName = [photo objectForKey:@"user"];
Person *contact = nil;
if (![contactNames containsObject:contactName]) {
contact = (Person *)[NSEntityDescription insertNewObjectForEntityForName:@"Person" inManagedObjectContext:managedObjectContext];
contact.name =contactName;
NSError *error;
if (![managedObjectContext save:&error]) {
NSLog(@"SHIT the save person FAILED!!! %@",error);
}
[contacts addObject:contact];
[contactNames addObject:contactName];
} else {
contact = [contacts objectAtIndex:[contactNames indexOfObject:contactName]];
}
[contactName release];
Photo *image = (Photo *)[NSEntityDescription insertNewObjectForEntityForName:@"Photo" inManagedObjectContext:managedObjectContext];
image.imageFile = [photo objectForKey:@"path"];
image.imageName = [photo objectForKey:@"name"];
image.owner = contact;
contact.photos = [NSSet setWithObjects:image,nil];
NSError *error;
if (![managedObjectContext save:&error]) {
NSLog(@"SHIT the save photoFAILED!!! %@",error);
}
[image release];
[contact release];
}
[plist release];
return [contacts autorelease];
}
, , .
.