程序收到信号“EXC_BAD_ACCESS”

时间:2012-12-28 07:18:46

标签: iphone ios uinavigationcontroller uitabbarcontroller

当我运行以下代码时,它会给出上述错误

- (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;
}

如果我运行以下代码,它运行良好

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;
我不明白这些之间有什么区别。在第一个我刚创建局部变量&将这些分配给属性。在后来的一个直接使用的属性。 为什么它给出错误程序接收信号“EXC_BAD_ACCESS”

4 个答案:

答案 0 :(得分:2)

我认为在第一个版本中你发布了一些代码,然后在你发布后再次发布了这样的对象:

[localTAbBarController release];这个。所以可能就是为什么你收到错误程序

发出信号“EXC_BAD_ACCESS”。在第二个你很好地使用你的对象没有发布所以它的工作

答案 1 :(得分:1)

<强>更新

嘿,我使用你的代码,你可以在这个波纹线上获得BAD ACCESS,参见..

[localNavigationController release];

只是评论它,你没有不可访问

答案 2 :(得分:0)

我得到了答案。因为多次释放相同的物体,它会发生。 我已经发布了[localNavigationController release];两次。以后一定是

[localFirstNavigationController release];

答案 3 :(得分:0)

请检查此行。

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

应为[localFistNavigationController release];