看板 MacDev 關於我們 聯絡資訊
我有一個tableViewController,然後我在leftBarButtonItem上加了個Button 這個button被clicked以後就會觸發下列method - (void)addPost:(id)sender { PostViewController *postViewController = [[PostViewController alloc] initWithUserID:userID]; postViewController.delegate = self; UINavigationController *postNavigationController = [[UINavigationController alloc] initWithRootViewController:postViewController]; [self.tabBarController presentModalViewController:postNavigationController animated:YES]; [postViewController release]; [postNavigationController release]; } <我遇到的第一個問題是> 每當我dismissModalViewController (也就是上述的postViewController)後 console就出現 EXEC_BAD_ACCESS 如果我comment最後兩行code中任一行, (也就是[postViewController release] or [postNavigationController release]) 就不會出現 EXEC_BAD_ACCESS 所以我就想看看retain count到底是多少 於是我就加入了一些NSLog - (void)addPost:(id)sender { PostViewController *postViewController = [[PostViewController alloc] initWithUserID:userID]; NSLog(@"retain count = %d", [postViewController retainCount]); postViewController.delegate = self; NSLog(@"retain count = %d", [postViewController retainCount]); UINavigationController *postNavigationController = [[UINavigationController alloc] initWithRootViewController:postViewController]; NSLog(@"retain count = %d", [postViewController retainCount]); NSLog(@"postNavigationController retain count = %d", [postViewController retainCount]); [self.tabBarController presentModalViewController:postNavigationController animated:YES]; NSLog(@"retain count = %d", [postViewController retainCount]); NSLog(@"postNavigationController retain count = %d", [postViewController retainCount]); [postViewController release]; NSLog(@"retain count = %d", [postViewController retainCount]); NSLog(@"postNavigationController retain count = %d", [postViewController retainCount]); //[postNavigationController release]; } 沒想到結果竟然是 2010-09-28 10:02:29.630 postViewController retain count = 1 2010-09-28 10:02:29.630 postViewController retain count = 1 2010-09-28 10:02:29.631 postViewController retain count = 3 2010-09-28 10:02:29.632 postNavigationController retain count = 3 2010-09-28 10:02:29.690 postViewController retain count = 26 2010-09-28 10:02:29.690 postNavigationController retain count = 26 2010-09-28 10:02:29.691 postViewController retain count = 25 2010-09-28 10:02:29.691 postNavigationController retain count = 25 怎麼會跳到26這麼多啊@@ 我看到都傻了..... 只好把問題放上來問問有沒有一點提示了QQ -- ※ 發信站: 批踢踢實業坊(ptt.cc) ◆ From: 114.136.207.246
Gsus:找出第一個問題了,發現如果comment"某些"code都可以解決 09/28 10:39
Gsus:BAD_ACCESS的時候,似乎通常代表是其他地方的問題 09/28 10:39
Gsus:以我這個例子來說,是在postViewController的dealloc裡頭多 09/28 10:40
Gsus:release了不該release的東西,不過retain count的部份還是qq 09/28 10:40
zonble:出現 bad access 的話,用 NSZombie 會比較好找 09/28 15:45
Gsus:我今天google了一下才知道可以用這東西...NSZombieEnabled.. 09/28 17:07