2012-12-28 101 views
1

當我運行此下面的代碼它給上面的錯誤程序接收到的信號「EXC_BAD_ACCESS」

- (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」

+0

化妝的NSLog您的視圖控制器的viewDidLoad中,然後檢查您是否能夠訪問此視圖 – Rajneesh071

+0

爲了EXC_BAD_ACCESS崩潰更容易調試進入斷點導航器,並添加一個異常斷點(使用默認設置)。 也在Scheme Editor中檢查「啓用殭屍對象」和「日誌例外」框。這將幫助您瞭解發生碰撞的位置。 – krafter

+1

使用ARC(自動引用計數)。沒有理由不這樣做。 –

回答

0

我有我的答案。由於不止一次釋放同一個對象,它就會發生。 我已經發布了[localNavigationController release];兩次。稍後它必須是

[localFirstNavigationController release]; 
2

我想在第一個你釋放一些代碼,然後後再次釋放你的對象,如:

[localTAbBarController release];這一點。所以可能這就是爲什麼你收到錯誤程序

信號「EXC_BAD_ACCESS」。在第二個你很好地使用你的對象沒有發佈,所以它的工作

罰款。

+0

不,甚至我直接分配tabBarController屬性&不使用任何發佈機制tabBarController它給出相同的錯誤 –

+0

我做了以下更改&試過,但同樣的錯誤self.tabBarController = [[UITabBarController alloc]在裏面]; [self.tabBarController setViewControllers:twoBars]; –

1

UPDATE:

嘿,我用你的代碼,在這裏你會得到BAD_ACCESS這個波紋線看..

[localNavigationController release]; 

只是評論它,你沒有BAD_ACCESS

+0

你能解釋一下這個背後的原因嗎? – Rajneesh071

+0

@ Rajneesh071嘿在這裏localNavigationController分配一次後它在這裏拉文德拉分解它,並在兩行後他又重新分配這個對象,不分配或不在類引用,所以在這裏它與第二次釋放localNavigationController崩潰.. –

0

只需檢查這一行。

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

應該[localFistNavigationController release];

相關問題