作者Gsus (肉元,吉米,助教)
看板MacDev
标题[问题] retain count的问题
时间Tue Sep 28 10:08:17 2010
我有一个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
1F:→ Gsus:找出第一个问题了,发现如果comment"某些"code都可以解决 09/28 10:39
2F:→ Gsus:BAD_ACCESS的时候,似乎通常代表是其他地方的问题 09/28 10:39
3F:→ Gsus:以我这个例子来说,是在postViewController的dealloc里头多 09/28 10:40
4F:→ Gsus:release了不该release的东西,不过retain count的部份还是qq 09/28 10:40
5F:→ zonble:出现 bad access 的话,用 NSZombie 会比较好找 09/28 15:45
6F:→ Gsus:我今天google了一下才知道可以用这东西...NSZombieEnabled.. 09/28 17:07