views:

55

answers:

2

I have a view with a scrollview. I use code to add labels to the scrollview. Since there's a lot of redundant code, I tried to move the label creation to a separate function that returns a UILabel. Here' that function:

- (UILabel *)   f_MakeLabelWithL:(float)MyLeft T:(float)MyTop W:(float)MyWidth H:(float)MyHeight Align:(UITextAlignment)MyAlign 
                      Font:(UIFont *)MyFont TextColor:(UIColor *)MyTextColor BGColor:(UIColor *)MyBGColor {

CGRect rect = CGRectMake(MyLeft, MyTop, MyWidth, MyHeight);
UILabel *label = [[[UILabel alloc] initWithFrame:rect] autorelease];

label.adjustsFontSizeToFitWidth = YES;
label.backgroundColor = MyBGColor;
label.font = MyFont;
label.numberOfLines = 0;
label.textAlignment = MyAlign;
label.textColor = MyTextColor;

return label;
}

I set the text of the label after it is returned. Or so I intended. In fact, a label that is told to have a blue background appears as a solid black rectangle. One with a clear background is entirely clear. Worse than that: the scrollview won't scroll, and attempting to make it do so crashes the app without any explanation in the debugger console.

Yet all this code works inline just fine and dandy.

Anyone know why?

Update: Putting the code inline fixed the display of the labels. But I was also using a function to create images:

- (UIImageView *) f_MakeImageWithL:(float)MyLeft T:(float)MyTop W:(float)MyWidth H:(float)MyHeight File:(NSString *)MyFile {
CGRect rect = CGRectMake(MyLeft, MyTop, MyWidth, MyHeight);
UIImageView *oImageView = [[[UIImageView alloc] initWithFrame:rect] autorelease];

NSString *s = [[Isystant f_DocumentsPath] stringByAppendingPathComponent:MyFile];
UIImage *oImage = [UIImage imageWithContentsOfFile:s];
[oImageView setImage:oImage];
oImageView.backgroundColor = [UIColor redColor];
oImageView.opaque = YES; // explicitly opaque for performance

return oImageView;
}

It was creating the images, but the frozen scroll/crash problem didn't go away until I put this code back inline also.

+1  A: 

Do you retain your returned Label somewhere?

hotpaw2
try not using autorelease, also try setting the text property to something before you return it to see if it helps.
Jesse Naugher
I do not retain the label. As soon as I add it to the scrollview's subview, I release it. Regarding autorelease, a function that creates and returns a label must autorelease it. Once the function returns the label, it would have no way to release it if it didn't autorelease it, thus it would leak memory.
Scott Pendleton
+1  A: 

Looks to me like you are over-releasing the UILabel. You shouldn't have to release the label after adding it to your scrollview if it was never retained anywhere prior to that (and you've autoreleased it in the makeImageWithL method).

That's probably why it is working when you put it inline... because in that scenario, I would bet you are removing the autorelease, right?

-S

Steve N
Right you are! I forgot that when I get a control from a function, I don't need to release it. What you don't alloc init, you don't release! My clue should have been the crash without any explanation in the debugger console. That seems always to happen when the problem is over-releasing. Much obliged. (Just for clarity: What works is to autorelease the control in the function, but not to release the object variable that was the receiver of the function.)
Scott Pendleton