The Leaks instrument is sounding the alarm on some code, but I don’t know how to address the leak without crashing the app. Here’s some code summarizing my approach, written some time ago and clearly in need of rethinking:
labels = [[NSMutableArray alloc] init];
for(int i = 0; i < 10; i++) {
// calculate x and y...
label = [[UILabel alloc] initWithFrame:CGRectMake(x, y, 70, 15)];
// customize label...
[labels addObject:label];
[label release];
[self addSubview:[labels objectAtIndex:i]];
}
Why bother with the labels NSMutableArray? Later, in other methods, I need to change the alpha of the labels, and it’s convenient to be able to say
[[labels objectAtIndex:num] setAlpha:0.5];
I believe the leak occurs because labels doesn’t get dealloc’ed during the normal app lifecycle, only when the superview is dealloc’d at quit.
Help!
Thanks.
It’s probably happening because you’re not dealloc’ing the labels array. What about releasing the labels array after the for loop, then using .tag to set the label on the UIView and find the label later via the .tag using viewWithTag to setAlpha?
Then later