为什么保留计数这么高?内存管理

发布于 2024-10-12 12:45:41 字数 1975 浏览 4 评论 0原文

我一直在回顾我的应用程序,试图处理所有内存问题并阅读内存管理。 我开始使用 [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 全日空[5653:207] 4
2011-01-17 19:47:21.286 全日空[5653:207] 7
2011-01-17 19:47:21.287 全日空[5653:207] 12
2011-01-17 19:47:21.301 ANA[5653:207] Load View

我不明白为什么更改这些属性或引用 navigationController 会导致保留计数激增。

我已经在没有自动释放和手动释放的情况下完成了它,但结果是相同的。 基本上我不明白,并且想知道retainCount命令是否可靠,因为如果我不能理解这一点,我认为我无法在其他地方调试任何内存问题......

I have been going back through my app trying to handle all the memory problems and reading up on memory management.
I began using [object retainCount] to trace my memory allocation.
Is this to be trusted because I keep finding the counts really strange?

Could someone explain the following:

Bear in mind this the app delegate and an empty mainViewController makes no difference.
The initWithRootViewController is causing the count to go up, but I don't see another way of adding one....

- (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]);

And this is the log ~

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

I don't understand why changing those properties or referencing the navigationController is causing the retain count to shoot up.

I have done it without the autoreleases and manually released too but the result is the same.
Basically I don't get it, and wonder if the retainCount command is reliable, because if I can't understand this, I don't think I can debug any memory issues elsewhere...

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

真心难拥有 2024-10-19 12:45:41

-retainCount的官方文档

重要提示:此方法通常对于调试内存管理问题没有任何价值。因为任意数量的框架对象可能保留了一个对象以保存对其的引用,而同时自动释放池可能在对象上保存任意数量的延迟释放,因此您不太可能从中获得有用的信息方法。

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

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

As stated in the official documentation for -retainCount,

Important: This method is typically of no value in debugging memory management issues. Because any number of framework objects may have retained an object in order to hold references to it, while at the same time autorelease pools may be holding any number of deferred releases on an object, it is very unlikely that you can get useful information from this method.

Don’t rely on -retainCount. Other objects may be retaining your object without you knowing it, and autoreleased objects might give you a wrong impression of the actual retain count.

You can rely on Apple’s framework objects to do the right thing and relinquish ownership of your object when appropriate, and you need to make sure you are doing that as well.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文