views:

70

answers:

1

Hi All,

I'm starting out with iPhone development, and have a bit of a problem with programming a UITableView which is a subpart of a regular UIViewController.

The Problem:

When tapping a row, the didSelectRowAtIndexPath generates an EXC_BAD_ACCESS (I've checked for the correct method signature. Whereas if I move the body of this method to willSelectRowAtIndexPath it works fine. This seems wrong to me as all the documentation/patterns use didSelectRowAtIndexPath. Ok, so whats really weird is whether the method is called at all. Surely if the following log output is not logged, then the method did not run, so I could put any code that compiles in there and it would simply not run. So why the exception? Plus I'm finding it hard to confirm that it has to do with a prematurely released object, since the program executes fine when moved to willSelectRowAtIndexPath. Maybe there is some object (re)allocation in between the will and did stages?

- (NSIndexPath *)tableView:(UITableView *)tableView willSelectRowAtIndexPath:(NSIndexPath *)indexPath {
        CommentViewController *commentViewController = [[CommentViewController alloc] initWithNibName:@"CommentViewController" bundle:nil];
        Comment *selectedComment = [[Comment alloc] init];
        selectedComment = [self.message.comments objectAtIndex:indexPath.row];
        commentViewController.comment = selectedComment;

        [self presentModalViewController:commentViewController animated:YES];

        [selectedComment release];
        [commentViewController release];    
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
        NSLog(@"This method is never called ... as I don't see this log output ... ");
}

The Setup:

The UITableView is created as an element in IB, and is wired as a reference variable in the UIViewController. The UIViewController is set to be the data source and the delegate of the UITableView. So far so good, the UIViewController view loads, and the UITableView is loaded with the cells correctly. So that means that the delegate methods for the data source have worked properly at least.

Next Step:

So now I want to click on a row and load up another view (either modally or whatever, it doesnt really matter right now) and BOOM, the simulator crashes with the EXC_BAD_ACCESS reported in the console.

Could really use some pointers on this. Thanks a mill for your help in advance!

Matt

+3  A: 

didSelectRowAtIndexPath is never being called in the code above because you evoke a modal view in willSelectRowAtIndexPath: which freezes the tableview and keeps it from sending messages to its delegate/datasource.

Here's the cause of your crash:

    Comment *selectedComment = [[Comment alloc] init]; // creates new object 
    selectedComment = [self.message.comments objectAtIndex:indexPath.row]; // assigns a different existing object. Newly created object never used and leaking
    commentViewController.comment = selectedComment; // set to assigned existing

    [self presentModalViewController:commentViewController animated:YES];

    [selectedComment release] // BINGO! you release the assigned object 

Your crash is caused by the BINGO line above. You're confusing the initialized Comment object with the other completely unrelated Comment object returned from the array. You release the Comment object returned from the array without ever having retained it. The object then dies and any attempt to access it subsequently causes the crash.

TechZen
Hey TechZen, thanks a mill for your help. I seemed to have been suffering from 2 things, indeed your advice helped, I moved the code back to did... and removed the will... method completely. I also had the strange effect that my breakpoints were not being respected, which I inviestigated here (http://www.innerexception.com/2008/05/in-xcode-orange-breakpoint-means-gdb.html). Finally the method would actually break and I could check whats going on. I'm still not 100% why it works now and not when I originally used did... to actually present the modal. Anyhoo thanks for your help!!!
Matthew