addSubview增量保留计数? [英] does addSubview increment retain count?

查看:141
本文介绍了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?

<删除>我有一个全局属性 @属性(非原子,保留)的UILabel * ingredientsTextLabel; 我在 viewDidLoad中实例化使用此代码:

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

我只有名为的属性,我的标题中没有属性,所以没有getter和setter。在我 viewDidLoad中

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!

现在我认为这样可行,然后在dealloc中我可以发布 ingredientsTextLabel ,但保留计数是2,所以在addSubview 以及之后我还需要释放i吗?我没有意识到这种情况发生了! :(

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! :(

推荐答案

是的,addSubview增加了保留计数。这是有道理的,因为该方法存储了不应该是的子视图释放/释放直到superview也被释放。当superview发布时,它还会释放它的所有子视图。

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.

这篇关于addSubview增量保留计数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆