6

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

Thomas Clayson
  • 29,657
  • 26
  • 147
  • 224

3 Answers3

10

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.

Felix
  • 576
  • 5
  • 5
  • Correct. Thomas, you don't need to do another release after addSubview: you should just worry to release ONCE per alloc/init. If addSubview: increases the retain count, it is his responsibility to release it. – pablasso Nov 12 '10 at 11:14
  • oh right... i see. Thank you. Does that mean that If I alloc/init a UIView, add it as a subview of `self.view` and then release it the retain count is still 1? Thus, even though I released it, can I still send it messages? Thanks – Thomas Clayson Nov 12 '10 at 11:46
  • Yes, that's correct. But if you store a reference to your UIView you should also retain this UIView. Just because this is the Cococa/Obj-C style. – Felix Nov 12 '10 at 12:23
3

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.

bbum
  • 162,346
  • 23
  • 271
  • 359
2

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.

Jason Lee
  • 3,200
  • 1
  • 34
  • 71
  • P.S. The colon at the end of the link is needed. – Jason Lee Dec 21 '11 at 02:12
  • +1 for answering the question, but you wrote "Actually it does". What does? What does it do? I'm confused! (Edit your answer using the "edit" link below your answer's text). – Shalom Craimer Dec 21 '11 at 06:53
  • Wow, only an answer over a year and a bit later than the question was asked, and indeed answered! Scraimer I think Jason's saying "Actually it does" to the question "does addSubview increment retain count?". Not that any of it matters anymore with the advent of automatic reference counting. :p – Thomas Clayson Dec 21 '11 at 11:26
  • Yes, "Actually it does" is for the question "does addSubview increment retain count?" – Jason Lee Dec 22 '11 at 07:12