如何修复奇怪的保留计数(1 个 init - 3 个保留计数)? +编辑:解除分配问题

发布于 2024-09-27 02:19:47 字数 1119 浏览 9 评论 0原文

所以我的代码是这样的:

ArticleControllerController *ac = [[ArticleControllerController alloc] init];
ac.categoryIndex = idx;
NSLog(@"acc retain: %d", [ac retainCount]);        
[app.nav pushViewController:ac animated:NO];
NSLog(@"acc retain: %d", [ac retainCount]);        
[ac release];
NSLog(@"acc retain: %d", [ac retainCount]);    

我得到:

[2649:207] acc retain: 1
[2649:207] acc retain: 3
[2649:207] acc retain: 2    

如何解决这个混乱?我不明白我做错了什么,这部分有时会因内存不足而导致应用程序崩溃。

编辑:相关问题。

所以情况与上面定义的相同,但问题是 ArticleControllerController dealloc 方法永远不会被调用。

更多代码:

- (void) navigateToNewsCategoryByIndex:(int)idx {
[app.nav popViewControllerAnimated:NO]; 

currentMode = MODE_ARTICLE;
ArticleControllerController *ac = [[ArticleControllerController alloc] init];
ac.categoryIndex = idx;
[app.nav pushViewController:ac animated:NO];
[ac release];
return ;        

}

如果此方法重复多次 ArticleControllerController 创建大量各种界面元素,但其 dealloc 方法永远不会释放它们(保留计数永远不会降至零),所以我认为这就是我正在尝试的内存崩溃问题现在要解决几天。

这是怎么回事?我可以做更多的事情来解决这个问题吗?

So my code goes like this:

ArticleControllerController *ac = [[ArticleControllerController alloc] init];
ac.categoryIndex = idx;
NSLog(@"acc retain: %d", [ac retainCount]);        
[app.nav pushViewController:ac animated:NO];
NSLog(@"acc retain: %d", [ac retainCount]);        
[ac release];
NSLog(@"acc retain: %d", [ac retainCount]);    

And I get:

[2649:207] acc retain: 1
[2649:207] acc retain: 3
[2649:207] acc retain: 2    

How to resolve this mess? I don't understand what I'm doing wrong and this part sometimes causes application crash due low memory.

Edit: related problem.

So the situation is the same as defined above, but the problem is that ArticleControllerController dealloc method never gets called.

More code:

- (void) navigateToNewsCategoryByIndex:(int)idx {
[app.nav popViewControllerAnimated:NO]; 

currentMode = MODE_ARTICLE;
ArticleControllerController *ac = [[ArticleControllerController alloc] init];
ac.categoryIndex = idx;
[app.nav pushViewController:ac animated:NO];
[ac release];
return ;        

}

If this method gets repeated several times ArticleControllerController creates massive amounts of various interface elements, but its dealloc method never releases them (retain count never goes down to zero), so I think here lies the memory-crash problem I am trying to resolve for a couple of days now.

What's up with that? Can I do something more to resolve this?

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

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

发布评论

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

评论(1

深海夜未眠 2024-10-04 02:19:47

这对我来说看起来不错。创建后,“ac”的保留计数为 1,这是正确的。然后你将它推送到 app.nav 并在那里计算另外两个保留,这很好。该类有责任释放它保留的任何内容。最后,您释放了创建的实例,并且保留计数下降到 2。但这两个计数不是您的责任,即 app.nav。

这就是为什么您不应该真正担心打印出retainCount,因为当您不知道另一个类的幕后情况时,它可能看起来很奇怪。

您需要做的就是为每个分配释放一个版本,并且您已经做到了

That looks fine to me. After you've created it 'ac' has a retain count of 1 which is correct. Then you push it to app.nav and in there a further two retains are counted which is fine. It is the responsibility of that class to release whatever it retains. Finally you release the instance you have created and the retain count goes down to 2. But those two counts are not your responsibility, that is app.nav.

This is why you should not really worry about printing out retainCount as it can look weird when you don't know what is going on behind the scenes in another class.

All you need to do is one release for every alloc and you have done that

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