繁体   English   中英

为什么保留人数如此之高? 内存管理

[英]Why is the retain count so high? Memory Management

我一直在浏览我的应用程序,尝试处理所有内存问题并阅读内存管理。 我开始使用[object keepCount]跟踪我的内存分配。 这是值得信赖的,因为我不断发现计数真的很奇怪吗?

有人可以解释以下内容:

请记住,此应用程序委托和一个空的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导致保留计数激增。

我没有自动发布就完成了它,也手动发布了,但是结果是一样的。 基本上我不了解它,不知道keepCount命令是否可靠,因为如果我不理解这一点,我认为我无法调试其他地方的任何内存问题...

-retainCount官方文档-retainCount

重要:在调试内存管理问题时,此方法通常没有任何价值。 由于可能有许多框架对象保留了一个对象以保留对其的引用,而同时自动释放池可能会在一个对象上保留任何数量的延迟发布,因此从此获取有用信息的可能性很小方法。

不要依赖-retainCount 其他对象可能会在您不知道的情况下保留您的对象,而自动释放的对象可能会给您对实际保留计数的错误印象。

您可以依靠Apple的框架对象来做正确的事情,并在适当的时候放弃对对象的所有权,并且需要确保自己也在做。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM