0

In my app, the user must answer a given question by inputting the answer within a table view. I have a table view that starts with one custom cell, in which the user inputs some data. That cell has a button that can load another custom cell, in which the user inputs some more data.

Once the user answers the question, the table view is modified so that it has only the first cell, for the new question. Also, several "data" arrays, including leftData.equation that are responsible for keeping track of the user's input are cleared of all objects. ( [array removeAllObjects] )

There is no crash when the second cell is loaded nor when I return to the first cell for the next question. However, when I try loading the second cell again, the program crashes with Bad Access. I'm assuming it has something to do with the array, leftData.equation

Below is the method called when the second cell loads

- (void) setUpBalanceCell: (UITableView *) tableView  {

    balanceCell = (BalanceCell *) [tableView dequeueReusableCellWithIdentifier:@"balanceCell"];

    if (balanceCell == nil) {   

        NSArray *nib =   [[NSBundle mainBundle] loadNibNamed:@"BalanceCell" owner:self options:nil];
        balanceCell = (BalanceCell*) [nib objectAtIndex:0];
        [balanceCell.rightButton addTarget:self action:@selector(test) forControlEvents:UIControlEventTouchUpInside];

    }
        // stores data from first cell into a data array
        for (FormulaLabel *label in equationCell.leftView.equationOrder) 
            [leftData.equation addObject:label.text];   //fix:  needs to comply with MVC   

        for (FormulaLabel *label in equationCell.rightView.equationOrder)  
            [rightData.equation addObject:label.text];  

        [leftData setUpBalancedEquation];
        [rightData setUpBalancedEquation];

        [self setUpView:balanceCell.leftView fromArray:leftData.equation toArray:leftBalanceItems]; // WHERE EXC_BAD ACCESS appears
        [self setUpView:balanceCell.rightView fromArray:rightData.equation toArray:rightBalanceItems];

}

The method where bad access is called

- (void)setUpView:(UIView *)view fromArray:(NSMutableArray *)equationData toArray:(NSMutableArray *)balanceItems {

int labelCount = 0; //label #
int startingPoint = 5; //x vaiue where first label starts

//Crashes in method below
for (NSString *equationText in equationData) {

    //add text
    FormulaLabel *tempLabel = [[FormulaLabel alloc] initWithFrame:CGRectMake(startingPoint, 2, 10, 22)];
    tempLabel.text = equationText;
    [tempLabel sizeToFit];
    [view addSubview:tempLabel];
    tempLabel.tag = labelCount;
    [balanceItems addObject:tempLabel];
    //set location of '+'
    startingPoint = tempLabel.frame.origin.x + tempLabel.frame.size.width + 3;

    if (labelCount != [equationData count]-1) { //not the last label

        UILabel *plus = [[[UILabel alloc] initWithFrame:CGRectMake(startingPoint, 5, 10, 10)]autorelease];
        plus.text = @"+";
        plus.font = [UIFont systemFontOfSize:13];
        [plus sizeToFit];
        [view addSubview:plus];
        startingPoint = plus.frame.origin.x + plus.frame.size.width + 3;
        [balanceItems addObject:plus];
    }

    labelCount ++;
    [tempLabel release];
}

}

Mahir
  • 1,684
  • 5
  • 31
  • 59
  • are you building with `NSZombieEnabled`? if not do so and see the error you get. http://stackoverflow.com/questions/2190227/how-do-i-setup-nszombieenabled-in-xcode-4 – KDaker Jul 02 '12 at 02:46
  • the specific error was -[__NSArrayM countByEnumeratingWithState:objects:count:]: message sent to deallocated instance 0x60270f0 which was in `for (NSString *equationText in equationData)` – Mahir Jul 02 '12 at 03:22
  • Not sure why it's saying the object is deallocated; like I said, all I called was `[self.equation removeAllObjects]` Or does that dealloc the object? – Mahir Jul 02 '12 at 03:27
  • no `removeAllObjects` does not release the array itself. have you tried commenting out `[self setUpView:balanceCell.leftView fromArray:leftData.equation toArray:leftBalanceItems];` wonder if you still get the error on the second call for `rightData`. check that, if it doesn't crash then the problem is probably happening elsewhere, not in the code mentioned above. – KDaker Jul 02 '12 at 03:51
  • yeah i tried commenting it out; the same error appears. I'll add more code above – Mahir Jul 02 '12 at 04:33
  • I found the error. Retaining `self.equation` stopped the crash. Memory management still confuses me; do you mind explaining why I needed to retain the array? – Mahir Jul 02 '12 at 05:11
  • `self` being left and right data – Mahir Jul 02 '12 at 05:19

1 Answers1

1

well, retaining it will definitely fix it, but if i were you i would look into where the array is being released in the first place.

Calling retain increments the retain count of an object. When you first create an object with [NSObject alloc] the retain count is set to 1. calling retain increases the count so it becomes 2. calling release, on the other hand, decrements the retain count by 1. so, assume you call release on an object with retain count of 1, it becomes 0. When it is 0, the object is released from memory (and calls dealloc).

I hope this makes things clear, memory management is very confusing. You might want to look into testing for memory leaks later on in your project.

best of luck.

Community
  • 1
  • 1
KDaker
  • 5,899
  • 5
  • 31
  • 44
  • The reason I was confused was that I never actually released the array. – Mahir Jul 02 '12 at 05:36
  • then thats it!! releasing leftData will also release the `equation` array. remember that you are calling the array as `leftData.equation`, even if equation is not released in leftData's `dealloc` method (which will cause a leak), releasing `leftData` means that you cannot access `leftData.equation` anymore. – KDaker Jul 02 '12 at 05:41
  • sorry, i updated the comment. It turns out that I never actually released leftData – Mahir Jul 02 '12 at 05:45
  • alright, well if it is taking too much time leave it the way it is. If you find memory management too confusing you can always look into using [ARC](http://www.raywenderlich.com/5677/beginning-arc-in-ios-5-part-1) . I never tried that myself though it should make things easier. good luck! – KDaker Jul 02 '12 at 05:50