我一直在回顾我的应用程序,试图处理所有内存问题并阅读内存管理。我开始使用 [object retainCount] 来跟踪我的内存分配。这是值得信任的,因为我一直发现计数真的很奇怪吗?
有人可以解释以下内容:
请记住,应用程序委托和空的 mainViewController 没有区别。initWithRootViewController 导致计数上升,但我没有看到另一种添加方法....
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
/* Create the View Controllers */
UIViewController *mainViewControl = [[[MainViewController alloc] init] autorelease];
/* Create the Navigation Controller */
UINavigationController *navigationController = [[[UINavigationController alloc] initWithRootViewController:mainViewControl] autorelease];
NSLog(@"retain count: %i",[mainViewControl retainCount]);
/* Set the toolbar to purple */
navigationController.navigationBar.barStyle = UIBarStyleBlackTranslucent;
navigationController.navigationBar.tintColor = [UIColor colorWithRed:.6 green:.1 blue:.4 alpha:0.4];
navigationController.navigationBar.translucent = YES;
NSLog(@"retain count: %i",[mainViewControl retainCount]);
navigationController.toolbar.barStyle = UIBarStyleBlackTranslucent;
navigationController.toolbar.tintColor = [UIColor colorWithRed:.6 green:.1 blue:.4 alpha:0.4];
navigationController.toolbar.translucent = YES;
[navigationController setNavigationBarHidden:YES animated:NO];
[navigationController setToolbarHidden:YES animated:NO];
NSLog(@"retain count: %i",[mainViewControl retainCount]);
[window addSubview:[navigationController view]];
NSLog(@"retain count: %i",[mainViewControl retainCount]);
这是日志~
2011-01-17 19:47:21.278 ANA[5653:207] 3
2011-01-17 19:47:21.282 ANA[5653:207] 4
2011-01-17 19:47:21.286 ANA[5653:207] 7
2011-01-17 19:47:21.287 ANA[5653:207] 12
2011-01-17 19:47:21.301 ANA[5653:207] Load View
我不明白为什么更改这些属性或引用 navigationController 会导致保留计数激增。
我在没有自动释放和手动释放的情况下完成了它,但结果是一样的。基本上我不明白,想知道retainCount命令是否可靠,因为如果我不能理解这一点,我认为我无法在其他地方调试任何内存问题......