6

I was getting this memory leak:

[UIPickerTableViewTitleCell initWithStyle:resuableIdentifier]; 

and

NSConcentrateMutableAttributedString.

Issue was that I had not implemented this delegate. After implementing this now memory leaks goes away. May be this information helpful for other as I spend mine 16 hours only to figure out this issue.

// Do something with the selected row.
- (UIView *)pickerView:(UIPickerView *)pickerView viewForRow:(NSInteger)row forComponent:(NSInteger)component reusingView:(UIView *)view {

// Get the text of the row.
NSString *rowItem = [NSString stringWithFormat:@"     %@",[machineData objectAtIndex:row]];

// Create and init a new UILabel.
// We must set our label's width equal to our picker's width.
// We'll give the default height in each row.
UILabel *lblRow = [[UILabel alloc] initWithFrame:CGRectMake(0.0f, 0.0f, [pickerView bounds].size.width, 44.0f)];

// Make the text color red.
[lblRow setTextColor: [UIColor blackColor]];
[lblRow setFont:[UIFont boldSystemFontOfSize:20]];

// Center the text.
[lblRow setTextAlignment:UITextAlignmentLeft];

// Add the text.
[lblRow setText:rowItem];

// Clear the background color to avoid problems with the display.
[lblRow setBackgroundColor:[UIColor clearColor]];

// Return the label.
return lblRow;
}
Irfan
  • 4,301
  • 6
  • 29
  • 46
Salman Iftikhar
  • 311
  • 2
  • 9
  • i don't know why, but this is true. I spent hours on searching for that memory leak. After I found this post I replaced pickerView:titleForRow:forComponent: with pickerView:viewForRow:forComponent:reusingView: , and the memory leak gone! Thanks a lot – almas Jun 05 '13 at 00:26

2 Answers2

0

Thanks for your info. Was confused by this leak. Only few comments:

  • probably lblRow should be autoreleased: return [lblRow autorelease];

  • [pickerView rowSizeForComponent:component] can be used to get size for new label.

Denis
  • 358
  • 5
  • 12
  • I am using ARC so that is why that makes life a bit easy. Secondly what i learn about ARC is that it release those first whose reference is nil. For example like lblRow = nil; But dont know what is best practice in case of "return". – Salman Iftikhar Nov 02 '12 at 12:04
0

I used IUIPicker in a popover and every time I dismissed the popover I had memory leak. I am also using ARC, so the easiest way I resolved this was by setting the UIPickerView = nil on unload. The following appears to have done the trick.

- (void)viewDidUnload
{
    [super viewDidUnload];
    // Release any retained subviews of the main view.
    // e.g. self.myOutlet = nil;
    self.pickerView = nil;
}
Ken
  • 1