Andrew Park
Andrew Park

Reputation: 1489

Incorrect decrement warning from Xcode from my class method

I created a class to take care of my UILabels in 1 line instead of taking 4-5 by doing...

+(UILabel*)BeautifyLabel:(UILabel *)label withText:(NSString *)message withFont:(NSString *)font andSize:(float)size andColor:(UIColor *)theColor{
    label.backgroundColor = [UIColor clearColor];
    label.textColor = theColor;
    label.font = [UIFont fontWithName:font size:size];
    label.text = message;
    return label;
}

And to call it, i do

UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake....];
label = [CommonMethods BeautifyLabel:label withText:@"hi" withFont:@"Helvetica" andSize:13 andColor:[UIColor whiteColor]];
[self.view addSubview label];
[label release];

The analyzer probably doesn't like the part where I pass the label to my CommomMethods class, but since i'm initializing and releases the label in the current controller and the CommonMethods class doesn't do anything memory related, this is safe, right?

Also, would this be cause for Apple to reject my app?

Thanks

Upvotes: 0

Views: 107

Answers (2)

zaph
zaph

Reputation: 112873

In the code:

UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake....];
label = [CommonMethods BeautifyLabel:label withText:@"hi" withFont:@"Helvetica" andSize:13 andColor:[UIColor whiteColor]];

label is allocated on the first line, on the second line label is replaed by the call to BeautifyLabel or so the analyzer thinks, not knowing what is done in BeautifyLabel. It can't assume you are returning the same object.

Either do not make the assignment:

[CommonMethods BeautifyLabel:label withText:@"hi" withFont:@"Helvetica" andSize:13 andColor:[UIColor whiteColor]];

or use different label pointer names:

UILabel *labelTemp = [[UILabel alloc] initWithFrame:CGRectMake....];
label = [CommonMethods BeautifyLabel:labelTemp withText:@"hi" withFont:@"Helvetica" andSize:13 andColor:[UIColor whiteColor]];

Upvotes: 0

progrmr
progrmr

Reputation: 77291

Your BeautifyLabel method should not return the label pointer. That is probably what the analyzer is complaining about (but it would be nice to see the text of the analyzer error).

The analyzer is assuming that BeautifyLabel method is returning a new instance of the label which then overwrites the one you had in label variable thus causing a memory leak of the overwritten instance (and overreleasing of the returned instance).

Upvotes: 2

Related Questions