8

I'm adding two subviews, which are stored by properties, to my view. When adding the subviews to my view the subViews are seemingly deallocated after my setup method is called. The end result is that the views never get displayed. Now, if I change my properties to strong as opposed to weak I retain a reference to the views and they now show on screen. So what's going on here? Why is the addSubview: and insertSubview: not retaining the subviews? See the code below:

btw, I'm using iOS5 with ARC (hence the strong and weak stuff)

#import "NoteView.h"
@interface NoteView() <UITextViewDelegate>
@property (weak, nonatomic) HorizontalLineView *horizontalLineView;  // custom subclass of UIView that all it does is draw horizontal lines
@property (weak, nonatomic) UITextView *textView;
@end

@implementation NoteView
@synthesize horizontalLineView = _horizontalLineView;
@synthesize textView = _textView;

#define LEFT_MARGIN 20
- (void)setup
{
    // Create the subviews and set the frames
    self.horizontalLineView = [[HorizontalLineView alloc] initWithFrame:self.frame];
    CGRect textViewFrame = CGRectMake(LEFT_MARGIN, 0, self.frame.size.width, self.frame.size.height);
    self.textView = [[UITextView alloc] initWithFrame:textViewFrame];


    // some addition setup stuff that I didn't include in this question...

    // Finally, add the subviews to the view
    [self addSubview:self.textView];
    [self insertSubview:self.horizontalLineView atIndex:0];
}

- (void)awakeFromNib
{
    [super awakeFromNib];

    [self setup];
}

- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        // Initialization code
        [self setup];
    }
    return self;
}
Joey J
  • 1,355
  • 10
  • 26

3 Answers3

21

Here's a line of your code:

self.horizontalLineView = [[HorizontalLineView alloc] initWithFrame:self.frame];

Recall that the horizontalLineView property is weak. Let's walk through what really happens in that line, with the extra code that ARC generates. First, you send the alloc and initWithFrame: methods, getting back a strong reference:

id temp = [[HorizontalLineView alloc] initWithFrame:self.frame];

At this point, the HorizontalLineView object has a retain count of 1. Next, because you used dot-syntax to set the horizontalLineView property, the compiler generates code to send the setHorizontalLineView: method to self, passing temp as the parameter. Since the HorizontalLineView property is declared weak, the setter method does this:

objc_storeWeak(&self->_horizontalLineView, temp);

That sets self->_horizontalLineView equal to temp, and puts &self->_horizontalLineView on the object's list of weak references. But it does not increment the retain count of the HorizontalLineView object.

Finally, because the temp variable is no longer needed, the compiler generates this:

[temp release];

That lowers the HorizontalLineView object's retain count to zero, so it deallocates the object. During deallocation, it walks down the list of weak references, and sets each one to nil. So self->_horizontalLineView becomes nil.

The way to fix this is to make the temp variable explicit, so that you can extend its lifetime until after you have added the HorizontalLineView object to its superview, which retains it:

HorizontalLineView *hlv = [[HorizontalLineView alloc] initWithFrame:self.frame];
self.horizontalLineView = hlv;
// ...
[self insertSubview:hlv atIndex:0];
rob mayoff
  • 375,296
  • 67
  • 796
  • 848
  • 2
    Wow - you're absolutely right! This is the exact kind of explanation I was hoping for on what is going on behind the scene. Doesn't it seem strange though that you have to go through the extra step of creating an explicit variable? In any case, thanks for the help! – Joey J Mar 17 '12 at 09:50
  • I found that it is enough to simply introduce a temp variable, you don't need to use it in the call to `insertSubview`. So you could use `[self insertSubview:self.horizontalLineView atIndex:0];` and it still works. This seems counter intuitive to me. Can you explain this behaviour? – Rory O'Bryan Oct 30 '12 at 12:39
3

Weak shouldn't be used unless in cases where a parent-child retain cycle is formed (the parent keeps a reference to the child, the child keeps a reference to the parent, so neither is dealloc'd). Strong is the ARC equivalent of retain (now invalid under ARC), and keeps a nice stable pointer to the object for a much longer period of time than a weak reference, hence addSubview actually works instead of giving you some kind of error.

Why is the addSubview: and insertSubview: not retaining the subviews?

Have you ever tried to call retain on a nil object? Yep, still nil. Your weak reference isn't keeping the UIView object around long enough to successfully 'call retain' on the object.

CodaFi
  • 43,043
  • 8
  • 107
  • 153
  • I mean I see what you are saying about how the views might not be kept around long enough to get retained, but the setup method should get called after the (parent) view is instantiated. So when I add my subviews to the view hierarchy they get retained, right? So I guess I'm still confused here. What am I missing? – Joey J Mar 17 '12 at 04:29
  • ... and Rob, I don't think it wouldn't crash - you are just sending messages to nil so that should be ok. – Joey J Mar 17 '12 at 04:29
  • Edited. Just a bit confused is all. Thanks Rob, and Jake. – CodaFi Mar 17 '12 at 04:31
0

Try using this instead:

    @property (nonatomic, retain)
Patrick T Nelson
  • 1,234
  • 12
  • 21