Okay so I am trying to use a simple UINavigationController with iPhone SDK in Xcode and it works all well when pushing but if go past 2 pushes and try to pop the view controllers I keep getting the error: EXC_BAD_ACCESS
I know what it means but how the heck do I fix it?
Here is my code… (Assume the MainViewController has a button that invokes the function showStartMenu)
FurballAppDelegate.h
//
// FurballAppDelegate.h
// Furball
//
// Created by Morgan Family on 7/28/10.
// Copyright __MyCompanyName__ 2010. All rights reserved.
//
#import <UIKit/UIKit.h>
@class MainViewController, StartViewController, SubjectViewController;
@interface FurballAppDelegate : NSObject <UIApplicationDelegate> {
UIWindow *window;
UINavigationController *navController;
MainViewController *mainController;
StartViewController *startController;
}
@property (nonatomic, retain) UIWindow *window;
@property (nonatomic, retain) UINavigationController *navController;
@property (nonatomic, retain) MainViewController *mainController;
@property (nonatomic, retain) StartViewController *startController;
- (void)popBack;
- (void)pushNext:(UIViewController *)next;
- (void)showStartMenu;
@end
FurballAppDelegate.m
//
// FurballAppDelegate.m
// Furball
//
// Created by Morgan Family on 7/28/10.
// Copyright __MyCompanyName__ 2010. All rights reserved.
//
#import "FurballAppDelegate.h"
#import "MainViewController.h"
#import "StartViewController.h"
#import "SubjectViewController.h"
@implementation FurballAppDelegate
@synthesize window;
@synthesize navController;
@synthesize mainController;
@synthesize startController;
- (void)applicationDidFinishLaunching:(UIApplication *)application {
window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
mainController = [[MainViewController alloc] init];
navController = [[UINavigationController alloc] initWithRootViewController:mainController];
[window addSubview:navController.view];
[window makeKeyAndVisible];
}
- (void)dealloc {
[mainController release];
[startController release];
[subjectController release];
[window release];
[super dealloc];
}
- (void)popBack {
[navController popViewControllerAnimated:YES];
}
- (void)pushNext:(UIViewController *)next {
[navController pushViewController:next animated:YES];
}
- (void)showStartMenu {
startController = [[StartViewController alloc] init];
[self pushNext:startController];
}
@end
StartViewController.h
//
// StartViewController.h
// Furball
//
// Created by Morgan Family on 8/4/10.
// Copyright 2010 __MyCompanyName__. All rights reserved.
//
#import
@interface StartViewController : UIViewController {
}
- (void)showSubjectMenu;
@end
StartViewController.m
//
// StartViewController.m
// Furball
//
// Created by Morgan Family on 8/4/10.
// Copyright 2010 __MyCompanyName__. All rights reserved.
//
#import "FurballAppDelegate.h"
#import "StartViewController.h"
#import "SubjectViewController.h"
@implementation StartViewController
- (void)loadView {
UIView *view = [[UIView alloc] initWithFrame:[UIScreen mainScreen].applicationFrame];
UIButton *btn = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[btn setFrame:CGRectMake(50, 50, 100, 30)];
[btn setTitle:@"DO WORK" forState:UIControlStateNormal];
[btn addTarget:self action:@selector(chooseSubject) forControlEvents:UIControlEventTouchUpInside];
[view addSubview:btn];
FurballAppDelegate *app = [[UIApplication sharedApplication] delegate];
UIButton *btnBack = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[btnBack setFrame:CGRectMake(50, 100, 100, 30)];
[btnBack setTitle:@"DO WORK" forState:UIControlStateNormal];
[btnBack addTarget:app action:@selector(popBack) forControlEvents:UIControlEventTouchUpInside];
[view addSubview:btnBack];
self.view = view;
[view release];
}
- (void)viewDidLoad {
[super viewDidLoad];
}
- (void)chooseSubject {
FurballAppDelegate *app = [[UIApplication sharedApplication] delegate];
SubjectViewController *subjectController = [[SubjectViewController alloc] init];
[app pushNext:subjectController];
}
- (void)dealloc {
[super dealloc];
}
@end
All the pushing in all of my files work. Even the “btnBack” when I touch it, pops the navigation controller back to MainViewController… but when I make a back button identical to the one on StartViewController, on the SubjectViewController it gives me that weird error.
I really appreciate any help 🙂
Since you realize it means you’re trying to access an invalid memory address, you need to examine your code for invalid memory accesses.
Fortunately, for this error, it’s typically right on the line where you receive the EXEC_BAD_ACCESS. Look at the objects and pointers on that line. Do they all make sense? If not, back up a line. Wash, rinse and repeat. Somewhere you’re not allocating an object properly, are releasing it too early, have stack corruption, or some variable pointing to random garbage.
Posting some of the code around where you receive the error might allow us to spot the error. However, it’s also possible that it’s impossible to see without being able to single-step through in the debugger.