Here is the line of your code:
self.horizontalLineView = [[HorizontalLineView alloc] initWithFrame:self.frame];
Recall that the horizontalLineView property is weak. Let me go through what really happens on this line, with the extra code that ARC generates. First you send the alloc and initWithFrame: methods, returning a strong link:
id temp = [[HorizontalLineView alloc] initWithFrame:self.frame];
At this point, the horizontalLineView object has a save value of 1. Further, since you used the dot syntax to set the horizontalLineView property, the compiler generates code to send the setHorizontalLineView: method to self , passing temp as a parameter. Since the horizontalLineView property is declared weak , the setter method does this:
objc_storeWeak(&self->_horizontalLineView, temp);
This sets self->_horizontalLineView to temp and puts &self->_horizontalLineView into the list of weak link objects. But it does not increment the save counter of the horizontalLineView object.
Finally, since the temp variable is no longer needed, the compiler generates this:
[temp release]
This lowers the value of the horizontalLineView to zero, so it frees the object. At the time of release, he iterates over the list of weak links and sets each of them to nil . So self->_horizontalLineView becomes nil .
The way to fix it is to make the temp variable explicit so that you can extend its lifetime until you add a horizontalLineView object to your supervisor that saves it:
HorizontalLineView *hlv = [[HorizontalLineView alloc] initWithFrame:self.frame]; self.horizontalLineView = hlv; // ... [self insertSubview:hlv atIndex:0];
rob mayoff
source share