addSubview增量是否保留计数?

发布于 2024-10-01 13:52:12 字数 1765 浏览 1 评论 0 原文

我已经测试过了,看起来确实如此。所以我的问题是,它是否总是增加保留计数。

所以每次我做这样的事情时:

UIView *theView = [[[UIView alloc] initWithFrame:(CGRect)aFrame] autorelease];
[self.view addSubview:theView];

我真的在泄漏内存吗?

我有一个全局属性@property(nonatomic,retain)UILabel *ingredientsTextLabel;,我用以下代码在viewDidLoad中实例化它:

我只有命名的属性,在我的标头中没有它的属性,因此没有 getter 和 setter。在我的viewDidLoad中:

    ingredientsTextLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, ingredientsScrollView.frame.size.width, ingredientsScrollView.frame.size.height)];
    [ingredientsTextLabel setBackgroundColor:[UIColor clearColor]];
    [ingredientsTextLabel setFont:[UIFont fontWithName:@"Helvetica" size:18]];
    [ingredientsTextLabel setText:ingredientsText];
    [ingredientsTextLabel setNumberOfLines:0];
    [ingredientsTextLabel setLineBreakMode:UILineBreakModeWordWrap];
    NSLog(@"%i",[ingredientsTextLabel retainCount]); // here retain count is 1

    CGSize maxSize = CGSizeMake(ingredientsScrollView.frame.size.width, 9999);
    CGSize ingLabSize = [ingredientsText sizeWithFont:ingredientsTextLabel.font
                                    constrainedToSize:maxSize
                                        lineBreakMode:ingredientsTextLabel.lineBreakMode];

    [ingredientsTextLabel setFrame:CGRectMake(ingredientsTextLabel.frame.origin.x, ingredientsTextLabel.frame.origin.x, ingredientsTextLabel.frame.size.width, ingLabSize.height)];
    [ingredientsScrollView addSubview:ingredientsTextLabel];
    NSLog(@"%i",[ingredientsTextLabel retainCount]); // here retain count is 2!

现在我认为这可以工作,然后在dealloc中我可以释放ingredientsTextLabel,但保留计数是2,所以我还需要在我之后释放i addSubview 以及?我没有意识到会发生这种事! :(

I've tested it and it looks like it does. So my question is, does it ALWAYS increment the retain count.

So everytime I do something like this:

UIView *theView = [[[UIView alloc] initWithFrame:(CGRect)aFrame] autorelease];
[self.view addSubview:theView];

Am I actually leaking memory?

I have a global property @property (nonatomic, retain) UILabel *ingredientsTextLabel; which I instantiate in viewDidLoad with this code:

I just have the property named, theres no property for it in my header, so no getter and setter. In my viewDidLoad:

    ingredientsTextLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, ingredientsScrollView.frame.size.width, ingredientsScrollView.frame.size.height)];
    [ingredientsTextLabel setBackgroundColor:[UIColor clearColor]];
    [ingredientsTextLabel setFont:[UIFont fontWithName:@"Helvetica" size:18]];
    [ingredientsTextLabel setText:ingredientsText];
    [ingredientsTextLabel setNumberOfLines:0];
    [ingredientsTextLabel setLineBreakMode:UILineBreakModeWordWrap];
    NSLog(@"%i",[ingredientsTextLabel retainCount]); // here retain count is 1

    CGSize maxSize = CGSizeMake(ingredientsScrollView.frame.size.width, 9999);
    CGSize ingLabSize = [ingredientsText sizeWithFont:ingredientsTextLabel.font
                                    constrainedToSize:maxSize
                                        lineBreakMode:ingredientsTextLabel.lineBreakMode];

    [ingredientsTextLabel setFrame:CGRectMake(ingredientsTextLabel.frame.origin.x, ingredientsTextLabel.frame.origin.x, ingredientsTextLabel.frame.size.width, ingLabSize.height)];
    [ingredientsScrollView addSubview:ingredientsTextLabel];
    NSLog(@"%i",[ingredientsTextLabel retainCount]); // here retain count is 2!

Now I thought this would work and then in dealloc I can release ingredientsTextLabel, but the retain count is 2, so do I need to also release i after I addSubview as well? I didn't realise this happens! :(

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

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

发布评论

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

评论(3

口干舌燥 2024-10-08 13:52:12

是的,“addSubview”增加了保留计数。这是有道理的,因为该方法存储子视图,在超级视图也被释放之前,子视图不应该被释放/释放。当超级视图被释放时,它也会释放它的所有子视图。

Yes, "addSubview" increases the retain count. This makes sense because the method stores the subview which should not be released/freed until the superview is also released. When the superview is release it also releases all its subviews.

耳钉梦 2024-10-08 13:52:12

不要使用-retainCount。

对象的绝对保留计数是没有意义的。

您应该调用 release 的次数与导致对象被保留的次数完全相同。不能少(除非你喜欢泄漏),当然也不能多(除非你喜欢崩溃)。

请参阅内存管理指南了解完整详细信息。

如果您 +new/+alloc/-retain/-copy (NARC) 对象,则需要平衡保留与释放(或自动释放)。故事结束。绝对保留计数,尤其是从框架类派生的类实例的绝对保留计数和/或传递到框架代码中,是一个实现细节,很可能不是您想要的。认为应该如此。

Do not use -retainCount.

The absolute retain count of an object is meaningless.

You should call release exactly same number of times that you caused the object to be retained. No less (unless you like leaks) and, certainly, no more (unless you like crashes).

See the Memory Management Guidelines for full details.

If you +new/+alloc/-retain/-copy (NARC) an object, you need to balance the retain with a release (or autoerelease). End of story. The absolute retain count, especially the absolute retain count of an instance of a class that is subclassed from a framework class and/or passed into framework code, is an implementation detail and quite likely to not be what you think it should be.

远山浅 2024-10-08 13:52:12

事实上确实如此。
您可以在 http://developer.apple.com/library/ios/documentation/uikit/reference/uiview_class/uiview/uiview.html#//apple_ref/occ/instm/UIView/addSubview

当然,superView 在 addSubview: 上保留了 subView,因此当删除 subView 时它会释放。

Actually it does.
You can refer this at http://developer.apple.com/library/ios/documentation/uikit/reference/uiview_class/uiview/uiview.html#//apple_ref/occ/instm/UIView/addSubview:

As a matter of course, the superView retains the subView on addSubview: , so it does release when removes the subView.

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