Tobias Tovedal
Tobias Tovedal

Reputation: 731

Function using to much memory

I have a function which is called by a button in a table view cell, after a 15-20 calls to the function (15 - 20 clicks on a button) I get a level 2 memory warning. Without this function I don't seem to get this error.

I've read up on the whole "memory responsibility" that comes with developing applications for iPhone but my inexperience is preventing me from implementing this on my function, so I'm hoping someone out there could take a look at it and see what might be sucking up all the memory.

The function (edited: "(int)sender" -> "(id)sender"):

- (void)addPoint:(id)sender {
if ([sender tag] == 0) {
    [playerOneScore replaceObjectAtIndex:holeNum withObject:[NSNumber numberWithInt:[[playerOneScore objectAtIndex:holeNum] intValue] + 1]];
} if ([sender tag] == 1) {
    [playerTwoScore replaceObjectAtIndex:holeNum withObject:[NSNumber numberWithInt:[[playerTwoScore objectAtIndex:holeNum] intValue] + 1]];
} if ([sender tag] == 2) {
    [playerThreeScore replaceObjectAtIndex:holeNum withObject:[NSNumber numberWithInt:[[playerThreeScore objectAtIndex:holeNum] intValue] + 1]];
} if ([sender tag] == 3) {
    [playerFourScore replaceObjectAtIndex:holeNum withObject:[NSNumber numberWithInt:[[playerFourScore objectAtIndex:holeNum] intValue] + 1]];
}
[tbl reloadData];
}

I'd be grateful for any pointers on how to release some memory from my function, the function is useless the way it's behaving right now.

Note: I've tried [button release] but this only seems to be creating errors and not really seem to help my memory situation.

Thank you in advance, Tobias Tovedal

EDIT: cellForRowAtIndex:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];


if (cell == nil) {
    cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier] autorelease];
}

[cell.detailTextLabel setText:[NSString stringWithFormat:@"%@", [players objectAtIndex:indexPath.row]]];
[cell.textLabel setFont:[UIFont systemFontOfSize:50]];

UIButton *plusBtn = [UIButton buttonWithType:UIButtonTypeRoundedRect];
plusBtn.frame = CGRectMake(255, 5, 60, 70);
[plusBtn setTitle:@"+" forState:UIControlStateNormal];
[plusBtn.titleLabel setFont:[UIFont systemFontOfSize:25]];
[plusBtn addTarget:self action:@selector(addPoint:) forControlEvents:UIControlEventTouchUpInside];

UIButton *minusBtn = [UIButton buttonWithType:UIButtonTypeRoundedRect];
minusBtn.frame = CGRectMake(190, 5, 60, 70);
[minusBtn setTitle:@"-" forState:UIControlStateNormal];
[minusBtn.titleLabel setFont:[UIFont systemFontOfSize:25]];
[minusBtn addTarget:self action:@selector(removePoint:) forControlEvents:UIControlEventTouchUpInside];

plusBtn.tag = indexPath.row;
minusBtn.tag = indexPath.row;

if (indexPath.row == 0) {
    [cell.textLabel setText:[[playerOneScore objectAtIndex:holeNum] stringValue]];
} if (indexPath.row == 1) {
    [cell.textLabel setText:[[playerTwoScore objectAtIndex:holeNum] stringValue]];
} if (indexPath.row == 2) {
    [cell.textLabel setText:[[playerThreeScore objectAtIndex:holeNum] stringValue]];
} if (indexPath.row == 3) {
    [cell.textLabel setText:[[playerFourScore objectAtIndex:holeNum] stringValue]];
}

[cell.contentView addSubview:plusBtn];
[cell.contentView addSubview:minusBtn];

return cell;
}

Upvotes: 0

Views: 137

Answers (3)

taskinoor
taskinoor

Reputation: 46037

You are creating and adding as subview plusBtn and minusBtn every time the method is called regarding the cell is reused or not. Adding as subview every time without removing the previous one is causing the leak. Try this:

if (cell == nil) {
    cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier] autorelease];

   UIButton *plusBtn = [UIButton buttonWithType:UIButtonTypeRoundedRect];
   plusBtn.frame = CGRectMake(255, 5, 60, 70);
   [plusBtn setTitle:@"+" forState:UIControlStateNormal];
   [plusBtn.titleLabel setFont:[UIFont systemFontOfSize:25]];
   [plusBtn addTarget:self action:@selector(addPoint:) forControlEvents:UIControlEventTouchUpInside];

   [cell.contentView addSubview:plusBtn];

   // do the same for minusBtn here
}

Upvotes: 0

Radu Lucaciu
Radu Lucaciu

Reputation: 706

You are adding the plusBtn and minusBtn each time the cell is displayed without ever removing them. Try moving the

[cell.contentView addSubview:plusBtn];
[cell.contentView addSubview:minusBtn];

code inside the block:

if (cell == nil) {
    cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier] autorelease];
}

Edit: Claus was faster

Upvotes: 0

Claus Broch
Claus Broch

Reputation: 9212

Every time you are accessing your table cell in cellForRowAtIndexPath: you are adding the minusBtn and plusBtn subviews.

You should rearrange your code so these subviews are only added to the cell when it is created. As it is now new buttons are added also if it is being reused and thus already contains these views.

Upvotes: 3

Related Questions