Pretty basic cellForRowAtIndexPath implementation.
I’m wondering why the analyzer is telling me that i’m leaking memory here.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}
MyObject *object = [[self.datasource objectForKey:key] objectAtIndex:indexPath.row];
cell.textLabel.text = object.title;
return cell;
}
the analyzer tells me that ‘cell’ is potentially leaking. Is this just a weakness in the analyzer or how am i supposed to handle this?
note: adding a call to autorelease like so:
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier] autorelease];
causes a crash. Any thoughts?
If you’re not using ARC, then you should autorelease the cell:
Note that this is not equivalent with autoreleasing it after retrieving it from
dequeueReusableCellWithIdentifier:, because that method is called multiple times for a particular cell, and over-autoreleasing causes the crash.