title: UITableView cell memory leak
body: <p>I am having a leak in my tableview that I am populating with data.  </p>
  
  <p>Here is what is happening:  </p>
  
  <p>I have an <code>NSMutableArray</code> filled with <code>NSDictionaries</code> (contact information) that I load from sql database each time the user would select a new index letter, the array is filled with info for those contacts.  </p>
  
  <p>Since I am using an instance variable array to hold the data I figure something like this before each time new data is swapped in:</p>
  
  <pre><code>if (currentConnections != nil) {
      [currentConnections release];
      currentConnections = nil;
  }
  </code></pre>
  
  <p>Would effectively get rid of my old data before I reset it.<br>
  However, this is where the strangeness begins. In my <code>tableView:cellForRowAtIndexPath:</code> I make calls like this:</p>
  
  <pre><code>NSString *firstname = [[currentConnections objectAtIndex:indexPath.row]
                         objectForKey:@"firstname"];
  UILabel *name = [[UILabel alloc]
                   initWithFrame:CGRectMake(75, 12, 190, 15)];
  name.font = [UIFont fontWithName:@"Gotham-Medium.ttf" 
                              size:12];
  name.textColor = [UIColor whiteColor];
  name.backgroundColor = [UIColor clearColor];
  name.text = firstname;
  </code></pre>
  
  <p>Where current connections is the array of dictionaries. I then add the <code>UILabel</code> as a subview of the cell:</p>
  
  <pre><code>[cell addSubview:name];
  [name release];
  </code></pre>
  
  <p>The behavior from this is such:  </p>
  
  <p>When I run as is (releasing current connections before each swap then loading the new data to it) the app will crash for a bad access error.<br>
  When I do not release the subviews I add to the cell the app does not crash but I watch in horror as live bytes continue to accumulate in the memory allocations tool until the point when I switch contact info enough that I get a memory warning then crash.  </p>
  
  <p>It seems to me that there is some weird memory ownership issues going on with the cell and subviews. I do specify the cell as reusable and assign it to autorelease before loading each with data.  </p>
  
  <p>Any ideas of what may be causing this leak?</p>
  
  <p>edit:</p>
  
  <p>This is the current tableView:cellForRowAtIndexPath:</p>
  
  <pre><code>- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
   {
  
  static NSString *MyIdentifier = @"MyIdentifier";
  UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:MyIdentifier];
  
  cell.accessoryType = UITableViewCellAccessoryNone;
  
  if (cell == nil) {
      NSLog(@"within");
      cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:MyIdentifier] autorelease];
  }
  
  if (!ALOCATED) {
  
  
  cellName = [[ UILabel alloc ]
                   initWithFrame:CGRectMake(75, 12, 190, 15)
                   ];
  cellName.font = [ UIFont 
               fontWithName:@"Gotham-Medium.ttf" 
               size:12 
               ];
  cellName.textColor = [ UIColor whiteColor ];
  cellName.backgroundColor = [ UIColor clearColor ];
  
  NSString *firstname = [[ currentConnections 
                          objectAtIndex:indexPath.row 
                          ] objectForKey:@"firstname" 
                         ];
  
  firstname = [ firstname stringByAppendingString:@" " ];
  
  NSString *lastname = [[ currentConnections 
                         objectAtIndex:indexPath.row 
                         ] objectForKey:@"lastname" 
                        ];
  
  UIImage *profPic = [[ currentConnections 
                       objectAtIndex:indexPath.row 
                       ] objectForKey:@"profilepicture" 
                      ];
  connectionPhoto = [[ UIImageView alloc ] initWithFrame:CGRectMake(10, 8, 56, 56) ];
  connectionPhoto.image = profPic;
  
  
  
  
  NSString *fullname = [ firstname stringByAppendingString:lastname ];
  
  cellName.text = fullname;
  
  
  [ cell addSubview:cellName ];
  
  
  
  cellTitle = [[ UILabel alloc ]
                   initWithFrame:CGRectMake(75, 31, 260, 13)
                   ];
  cellTitle.font = [ UIFont 
               fontWithName:@"ArialMT" 
               size:10 
               ];
  cellTitle.textColor = [ UIColor whiteColor ];
  cellTitle.backgroundColor = [ UIColor clearColor ];
  cellTitle.text = [[ currentConnections 
                    objectAtIndex:indexPath.row 
                    ] objectForKey:@"title" 
                   ];
  
  
  [ cell addSubview:cellTitle ];
  [ cell addSubview:connectionPhoto ];
  [ profPic release ];
  
      ALOCATED = YES;
  
  } else {
  
  
  
      cellTitle.text = [[ currentConnections 
                         objectAtIndex:indexPath.row 
                         ] objectForKey:@"title" 
                        ];
  
      connectionPhoto.image = [[ currentConnections 
                                                               objectAtIndex:indexPath.row 
                                                               ] objectForKey:@"profilepicture" 
                               ];
  
  
      NSString *firstname = [[ currentConnections 
                              objectAtIndex:indexPath.row 
                              ] objectForKey:@"firstname" 
                             ];
  
      firstname = [ firstname stringByAppendingString:@" " ];
  
      NSString *lastname = [[ currentConnections 
                             objectAtIndex:indexPath.row 
                             ] objectForKey:@"lastname" 
                            ];
      NSString *fullname = [ firstname stringByAppendingString:lastname ];
  
      cellName.text = fullname;
  
  }
  
  return cell;
  
  }
  </code></pre>
  
  <p>Where I am attempting to create one cell and reuse it, only changing the properties of its subviews each time.</p>
  
  <p>EDIT</p>
  
  <p>What was happening was that I was adding subviews to the cell object itself, which was causing a leak as these were not being deallocated. As soon as I started adding the views to the UITableViewCell member views aka cell.contentView, cell.accessoryView etc... the memory was released. So as daniel mentioned below, either create a custom table cell and add your views to your cell properties or add your views to the member uitablecellviews not the cell itself. The following worked for me:</p>
  
  <pre><code>   - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
  {
  
  static NSString *MyIdentifier = @"MyIdentifier";
  UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:MyIdentifier];
  
  cell.accessoryType = UITableViewCellAccessoryNone;
  
  if (cell == nil) {
  
      cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:MyIdentifier] autorelease];
  }
  
  
  
  
  UILabel *cName = [[ UILabel alloc ]
                   initWithFrame:CGRectMake(105, 10, 190, 15)
                   ];
  cName.font = [ UIFont 
               fontWithName:@"Gotham-Medium.ttf" 
               size:12 
               ];
  cName.textColor = [ UIColor whiteColor ];
  cName.backgroundColor = [ UIColor clearColor ];
  
  NSString *firstname = [[ currentConnections 
                          objectAtIndex:indexPath.row 
                          ] objectForKey:@"firstname" 
                         ];
  
  firstname = [ firstname stringByAppendingString:@" " ];
  
  NSString *lastname = [[ currentConnections 
                         objectAtIndex:indexPath.row 
                         ] objectForKey:@"lastname" 
                        ];
  
  NSString *fullname = [ firstname stringByAppendingString:lastname ];
  
  cName.text = fullname;
  
  [ cell.contentView addSubview:cName ];
  [ cName release ];
  
  
  
  cell.imageView.image = [[ currentConnections 
                       objectAtIndex:indexPath.row 
                       ] objectForKey:@"profilepicture" 
                      ];
  
  
  
  UILabel *cTitle = [[ UILabel alloc ]
                   initWithFrame:CGRectMake(105, 25, 190, 13)
                   ];
  cTitle.font = [ UIFont 
               fontWithName:@"ArialMT" 
               size:10 
               ];
  cTitle.textColor = [ UIColor whiteColor ];
  cTitle.backgroundColor = [ UIColor clearColor ];
  cTitle.text = [[ currentConnections 
                    objectAtIndex:indexPath.row 
                    ] objectForKey:@"title" 
                   ];
  
  
  [ cell.contentView addSubview:cTitle ];
  [ cTitle release ];
  
  return cell;
  
  }
  </code></pre>