I have this UITableView app, a basic diary app, in this method XCode says I have a memory leak. The first line that leak is suggested to start is 117 “NSString *CellIdentifier”.
Down to if (cell ==…, to Diary *diaryEntry, to NSString *stringDate.
There it states that the Method returns an object with a +1 retain count, owning.
I’ve tried to release cell, stringDate… nothing changes this fact, so what am I thinking/doing wrong here?
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
// A date formatter for the time stamp static
static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier] autorelease];
}
Diary *diaryEntry = [diaryArray objectAtIndex:indexPath.row];
cell.textLabel.text = diaryEntry.diaryTitle;
NSString *stringDate = [[DiaryDateFormatter alloc] convertDateToString:[diaryEntry diaryDate]];
cell.detailTextLabel.text = stringDate;
[stringDate release];
return cell;
}
This line looks problematic:
convertDateToString:is an initialiser method, then it should be renamed. However, it looks likeconvertDateToString:should be a class method anyway (this way you won’t have to allocate instances of theDiaryDateFormatterclass).allocmethod always gives you an object with a +1 retain count. Therefore it is your responsibility to release it. The way your code is set up means you can no longer access the allocated object in order to release it.Try changing the above line of code into this:
Also, don’t release
stringDate, because it was not obtained using a method whose name implies that you have ownership.