简体   繁体   中英

program received signal “EXC_BAD_ACCESS”

When I run this following code it gives above error

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{


   self.window = [[[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]] autorelease];

    [self.window makeKeyAndVisible];

    self.viewController = [[ViewController alloc] initWithNibName:nil bundle:nil];
    self.firstViewController=[[FirstViewController alloc]initWithNibName:nil bundle:nil];
    UINavigationController *localNavigationController=[[UINavigationController alloc]initWithRootViewController:self.viewController];
    self.navigationController=localNavigationController;
    [localNavigationController release];
    UINavigationController *localFistNavigationController=[[UINavigationController alloc]initWithRootViewController:self.firstViewController];
    self.firstNavigationController=localFistNavigationController;
    [localNavigationController release];
   NSArray *twoBars=[[NSArray alloc]initWithObjects:self.navigationController,self.firstNavigationController, nil];
    UITabBarController *localTAbBarController =[[UITabBarController alloc]init];
    [localTAbBarController setViewControllers:twoBars];
    self.tabBarController=localTAbBarController;
    [localTAbBarController release];
    [self.window addSubview:self.tabBarController.view];

        return YES;
}

if i run following code it runs well

self.window = [[[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]] autorelease];
         [self.window makeKeyAndVisible];

    self.viewController = [[ViewController alloc] initWithNibName:nil bundle:nil];
    self.firstViewController = [[FirstViewController alloc] initWithNibName:nil bundle:nil];

    self.navigationController = [[UINavigationController alloc]
                                 initWithRootViewController:self.viewController];

    self.firstNavigationController=[[UINavigationController alloc]initWithRootViewController:self.firstViewController];
    NSArray *twoBars=[[NSArray alloc]initWithObjects:self.navigationController,self.firstNavigationController, nil];

    self.tabBarController=[[UITabBarController alloc]init];
    [self.tabBarController setViewControllers:twoBars];

       [self.window addSubview:self.tabBarController.view];



    return YES;

i not understood what is the difference between these. in first one i just created local variables & assigned those to properties. in later one directly used the properties. why it is giving the error- program recieved signal "EXC_BAD_ACCESS"

I think in first one you releases some code and then after release you again that object like:

[localTAbBarController release]; this. So may be thats why you got error- program recieved

signal "EXC_BAD_ACCESS". in second one you nicely use your object no releases so its work

fine.

UPDATE:

hey i use your code, here you get BAD_ACCESS on this bellow line see..

[localNavigationController release];

just comment it and you have not BAD_ACCESS

i got my answer. because of releasing same objects more than one time, it happens. i have released [localNavigationController release]; two times. later it must be

[localFirstNavigationController release];

Just check this line.

self.firstNavigationController=localFistNavigationController;
  -->>  [localNavigationController release];  

It should be [localFistNavigationController release];

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM