最近创建的对象的保留计数较大。 Objective-C
我遇到了一个奇怪的情况,即按下按钮时正在加载的视图控制器的保留计数过多。
代码如下:
-(IBAction)new
{
if (!viewSpace)
viewSpace = [[ViewSpace alloc] initWithNibName:@"ViewSpace" bundle:nil];
viewSpace.delegate = self;
viewSpace.view.frame = CGRectMake(0, 0, viewSpace.view.frame.size.width, viewSpace.view.frame.size.height);
[self presentModalViewController:viewSpace animated:YES];
NSLog(@"Count Retain: %d",[viewSpace retainCount]);
}
-(void)viewSpaceWasDissmissed:(id)sender
{
[self dismissModalViewControllerAnimated:YES];
[viewSpace release];
NSLog(@"Count Retain: %d",[viewSpace retainCount]);
}
第一次执行IBAction New时,刚创建时保留计数为5。 (必须是1)。
当必须卸载 ViewSpace 对象时,调用 viewSpaceWasDismissed 函数以删除模态视图并释放先前的对象。
问题是,保留计数永远不会达到0,并且ViewSpace的dealloc方法永远不会被调用,从而导致内存泄漏。
我的问题是最近创建的 ViewController 怎么可能有 5 个保留?我确保以前从未创建过它。
谢谢。
I'm getting an strange case of excessive retain counts for a view controller that I'm loading when a button is pushed.
This is the code:
-(IBAction)new
{
if (!viewSpace)
viewSpace = [[ViewSpace alloc] initWithNibName:@"ViewSpace" bundle:nil];
viewSpace.delegate = self;
viewSpace.view.frame = CGRectMake(0, 0, viewSpace.view.frame.size.width, viewSpace.view.frame.size.height);
[self presentModalViewController:viewSpace animated:YES];
NSLog(@"Count Retain: %d",[viewSpace retainCount]);
}
-(void)viewSpaceWasDissmissed:(id)sender
{
[self dismissModalViewControllerAnimated:YES];
[viewSpace release];
NSLog(@"Count Retain: %d",[viewSpace retainCount]);
}
When the IBAction New is executed first time, the retain count is 5 when just is created. (It must be 1).
When the ViewSpace object must be unload calls viewSpaceWasDismissed function in order to remove the modal view and release the previous object.
The problem is that never the retain count reach 0 and the dealloc method of ViewSpace never is called causing memory leaks.
My question is how is possible that a recently created ViewController have 5 retains? I made sure that is never created before.
Thanks.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
Cocoa 可能由于其自身原因在内部保留了 4 次视图控制器。这不是问题。
更一般地说,由于类似的原因,-retainCount 方法是无用的,并且您永远不应该调用它。它不会帮助你,而且会让你感到困惑。
为了调试泄漏,我建议使用leaks Instrument,检查对象,并分析每个保留和释放的来源,以确定是否有错误。
Cocoa is probably retaining the view controller 4 times internally for reasons of its own. This isn't a problem.
More generally, the -retainCount method is useless for reasons like this, and you should never call it. It will not help you, and it will confuse you.
To debug your leak, I suggest using the leaks Instrument, inspecting the object, and analyzing where each retain and release is coming from to determine whether any are incorrect.
查看
-retainCount
的文档。我相信它说你不应该自己调用它 - 你只需要处理你引起的任何保留,并且不用担心“实际”保留计数。Check the documentation for
-retainCount
. I believe it says that you should not be calling it yourself - you just need to take care of any retains that you cause, and don't worry about the 'actual' retain count.您在这里做错了两件事:
viewSpace
呈现后释放它,并且不需要dismissModalViewController
方法中的释放消息。顺便说一句,ViewSpace
对于视图控制器来说是一个糟糕的名称。在我知道它是一个视图控制器之前,我必须读到您将其呈现为视图控制器的行。我认为 ViewSpaceController 是一个更具描述性的名称。retainCount
,这始终是一个坏主意。重要的是,在您的new
方法中,您创建了一个拥有的对象(使用alloc
),并且您通过释放来平衡该所有权(或者至少当您输入我在第 1 点中建议的更正,就是这样。您获得了一个对象的所有权并释放了它。retainCount
方法绝对不会告诉您任何对您有任何用处的信息。不要这样做。只需平衡所有权与发布,这才是最重要的。You're doing two things wrong here:
viewSpace
after it is presented, and you don't need the release message in thedismissModalViewController
method. As an asideViewSpace
is a poor name for a view controller. I had to read to the line where you are presenting it as a view controller before I knew it was a view controller. I thinkViewSpaceController
is a more descriptive name.retainCount
which is always a bad idea. All that matters is that in yournew
method you created an owned object (with thealloc
) and you balanced that ownership with a release (or at least you will do when you put in the correction I suggested in point 1) That's it. You took ownership of an object and you released it. TheretainCount
method tells you absolutely nothing that can be of any use to you. Don't do it. Just balance ownerships with release, and that is all that matters.我不是 100% 确定每个计数,但这里有一些:
此外,将其列为强属性的任何属性(在 ARC 中)。
我注意到,当您启动笔尖并在笔尖设计中使用控制器的组件时,它将增加控制器实例上的引用计数(以强方式)。
I'm not 100% sure of every count but here are some:
Additionally any properties that list it as a strong property (in ARC).
I noticed that when you launch a nib and you use components of the controller in the nib design, it will increase reference counts (in a strong manner) on the controller instance.