最近创建的对象的保留计数较大。 Objective-C

发布于 2024-12-28 20:05:11 字数 903 浏览 2 评论 0原文

我遇到了一个奇怪的情况,即按下按钮时正在加载的视图控制器的保留计数过多。

代码如下:

-(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 技术交流群。

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

发布评论

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

评论(4

知你几分 2025-01-04 20:05:11

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.

幸福还没到 2025-01-04 20:05:11

查看 -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.

旧故 2025-01-04 20:05:11

您在这里做错了两件事:

  1. 当前视图控制器保留模态呈现的视图控制器并在关闭时释放它。因此,您应该在 viewSpace 呈现后释放它,并且不需要 dismissModalViewController 方法中的释放消息。顺便说一句,ViewSpace 对于视图控制器来说是一个糟糕的名称。在我知道它是一个视图控制器之前,我必须读到您将其呈现为视图控制器的行。我认为 ViewSpaceController 是一个更具描述性的名称。
  2. 您正在使用retainCount,这始终是一个坏主意。重要的是,在您的 new 方法中,您创建了一个拥有的对象(使用 alloc),并且您通过释放来平衡该所有权(或者至少当您输入我在第 1 点中建议的更正,就是这样。您获得了一个对象的所有权并释放了它。 retainCount 方法绝对不会告诉您任何对您有任何用处的信息。不要这样做。只需平衡所有权与发布,这才是最重要的。

You're doing two things wrong here:

  1. The Current view controller retains the modally presented view controller and releaseds it when it is dismissed. So you should release viewSpace after it is presented, and you don't need the release message in the dismissModalViewController method. As an aside ViewSpace 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 think ViewSpaceController is a more descriptive name.
  2. You are using retainCount which is always a bad idea. All that matters is that in your new method you created an owned object (with the alloc) 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. The retainCount 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.
百善笑为先 2025-01-04 20:05:11

我不是 100% 确定每个计数,但这里有一些:

  • 实例化 - 1
  • NIB - 1+
  • 强属性 (1+)

此外,将其列为强属性的任何属性(在 ARC 中)。
我注意到,当您启动笔尖并在笔尖设计中使用控制器的组件时,它将增加控制器实例上的引用计数(以强方式)。

I'm not 100% sure of every count but here are some:

  • Instantiation - 1
  • NIB - 1+
  • Strong Properties (1+)

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.

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