views:

26

answers:

1

So i have this very basic ipad view controller and i was doing some testing with multiple UITableViews in the same view. The issue I was having was when I selected a row, it would throw a EXC_BAD_ACCESS so I turned on the stack logging and found this

*** -[VCTables tableView:didSelectRowAtIndexPath:]: message sent to deallocated instance 0x4c0ad40

Then i started looking at my code and could not figure out for the life of me what is causing it. I have UITableViewDataSource and UITableViewDelegate on my view controller and have all the appropriate code to handle did select row at index. It draws the tables properly, it just doesnt seem to be hitting self as the datasource.

I have tried building the UITableViews in code, and also in Interface builder. I have re-downloaded and installed XCode 3.2.3 SDK 4.0.2 after uninstalling it and restarting. I couldn't for the life of me see what I am missing but after doing the previous, I am convinced now (i guess) that it is a code issue rather than the IDE, I just cant open my eyes wide enough to see the code issue.

Also, this happens with just one table as well. And with 2 tables, it happens no matter which table I select

here is some code:

VCTables.h

#import <UIKit/UIKit.h>
@interface VCTables : UIViewController<UITableViewDelegate,UITableViewDataSource> {
    UITableView *table1;
    UITableView *table2;    
}
@end

VCTables.m

#import "VCTables.h"
@implementation VCTables
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 50;
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return 7;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    NSString *CellIdentifier = [[NSString alloc] initWithString:@"yourCell"];
    if(tableView.tag == 2000){
        [CellIdentifier release];
        CellIdentifier = [[NSString alloc] initWithString:@"myCell"];
    }
    UITableViewCell *cell = (UITableViewCell*)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
    }
    if(tableView.tag == 2000){
        [cell.textLabel setText:[NSString stringWithFormat:@"%d",indexPath.row]];
    }else{
        [cell.textLabel setText:[NSString stringWithFormat:@"%d%d",indexPath.row,indexPath.section]];
    }
    [CellIdentifier release];   
    return cell;
}
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath {
    return NO;
}
- (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath {
    return NO;
}
-(void)tableView:(UITableView*)tableView didSelectRowAtIndexPath:(NSIndexPath*)indexPath {
    NSLog(@"selected"); 
}
- (void)viewDidLoad {
    [super viewDidLoad];
    table1 = [[UITableView alloc] initWithFrame:CGRectMake(20, 73, 320, 480) style:UITableViewStylePlain];
    table1.delegate=self;
    table1.dataSource=self;
    table1.tag=2000;
    [self.view addSubview:table1];
    table2 = [[UITableView alloc] initWithFrame:CGRectMake(483, 73, 320, 480) style:UITableViewStylePlain];
    table2.delegate=self;
    table2.dataSource=self;
    table2.tag=2000;
    [self.view addSubview:table2];
}
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
    return YES;
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
}
- (void)viewDidUnload {
    [super viewDidUnload];
}
- (void)dealloc {
    [super dealloc];
}
@end

I'm not sure it gets more basic than that. Please tell me the rookie mistake that is so painfully obvious that I must stop posting here. Thanks in advance

+3  A: 

message sent to deallocated instance 0x4c0ad40

This error indicates that your VCTables instance has been released. The UITableView still has a reference to it (the delegate property), so it is trying to send a message to a released object. The common term for this is a zombie.

In order to debug, you should look at how the memory management is being done for your VCTables object. Where is it created, and who owns it?

If you can make use of Apple's performance tools, try using the zombies tool to find out where that VCTables object is released.

e.James
omg, speaking of zombies, I sent VCTables to view and then just released it because i just alloc'd it and wasn't thinking any further... Thanks for reassuring to me that I am not crazy!
AtomRiot
No worries. We've all been there `:)`
e.James