EXC_BAD_ACCESS Problem With Release Statement
I don't know what is wrong with that statement because I have exact开发者_C百科ly the same code which I copied from another project of mine but it just keeps throwing EXC_BAD_ACCESS when everything looks fine. Can anybody pinpoint the problem? Much greatly appreciated as I have been on this error for hours, the same code keeps producing different results such as either bad access or I get a white screen.
Code Fragment:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions{
NSLog(@"0");
UINavigationController * localNavigationController;
tabBarController = [[UITabBarController alloc] init];
NSMutableArray * localControllersArray = [[NSMutableArray alloc] initWithCapacity:4];
//ProductViewController
ProductViewController * productViewController;
productViewController = [[ProductViewController alloc] initWithTabBar];
localNavigationController = [[UINavigationController alloc] initWithRootViewController: productViewController];
[localControllersArray addObject:localNavigationController];
NSLog(@"1");
// memory statements
[localNavigationController release];
[productViewController release];
//Search View Controller
SearchViewController * searchViewController;
searchViewController = [[SearchViewController alloc] initWithTabBar];
localNavigationController = [[UINavigationController alloc] initWithRootViewController: searchViewController];
[localControllersArray addObject:localNavigationController];
// memory statements
[localControllersArray release];
[searchViewController release];
NSLog(@"2");
//Register View Controller
RegisterViewController * registerViewController;
registerViewController = [[RegisterViewController alloc] initWithTabBar];
localNavigationController = [[UINavigationController alloc] initWithRootViewController:registerViewController];
[localControllersArray addObject:localNavigationController];
NSLog(@"3");
//memory management
[localControllersArray release];
[registerViewController release];
//About View Controller
AboutViewController * aboutViewController;
aboutViewController = [[AboutViewController alloc] initWithTabBar];
localNavigationController = [[UINavigationController alloc] initWithRootViewController: aboutViewController];
[localControllersArray addObject:localNavigationController];
//memory management
[localNavigationController release];
[aboutViewController release];
NSLog(@"4");
// Override point for customization after application launch.
tabBarController.viewControllers = localControllersArray;
[window addSubview:tabBarController.view];
// Override point for customization after application launch.
[self.window makeKeyAndVisible];
return YES;
The NSLog was to see where was the problem. The 3 doesn't show up after [localNavigationController release].
I have attached the project.
http://www.mediafire.com/?eauye5s361cyej0
Thanks in advance.
Use the static analyzer
Really, use "Analyze" from the product menu, and it will pin-point the problem within a second.
In
[localControllersArray addObject:localNavigationController];
you use the array that you released some lines before.
Edit: And yes, posting code instead of upload a project is the way to go. Guess I just happen to have a nice day and downloaded.
The error is with:
[localControllersArray release];
just after your NSLog(@"3");
statement.
localControllersArray
has already been released before the NSLog(@"2")
statement.
The solution is to remove the [localControllersArray release];
before the NSLog(@"2")
statement.
精彩评论