views:

47

answers:

2

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;

}

A: 

I see the problem. You're using objective-c, and you're no good at it (like me!)

More to the point, IIRC you should be using autorelease instead of alloc for the cell if you're going to return it? That's all I remember before I jumped ship...

x0n
+1  A: 

This line looks problematic:

NSString *stringDate = [[DiaryDateFormatter alloc] convertDateToString:[diaryEntry diaryDate]];
  1. Once you've allocated an instance of a class, you need to initialise it. If convertDateToString: is an initialiser method, then it should be renamed. However, it looks like convertDateToString: should be a class method anyway (this way you won't have to allocate instances of the DiaryDateFormatter class).
  2. The alloc method 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:

DiaryDateFormatter *formatter = [[DiaryDateFormatter alloc] init];
NSString *stringDate = [formatter convertDateToString:[diaryEntry diaryDate]];
[formatter release];

Also, don't release stringDate, because it was not obtained using a method whose name implies that you have ownership.

dreamlax
That I finally managed to figure out, to ages it felt like. But now I have killed that covertDateToSting thing. Just caused problems. After that problem solved.
swe_mattias