views:

107

answers:

1

Hello, I'm having a bear of a time trying to figure out why I'm getting a EXC_BAD ACCESS error. The console is giving me this eror: " -[CFArray objectAtIndex:]: message sent to deallocated instance 0x3b14110", I Can't figure it out...Thanks in advance.

// Customize the number of rows in the table view.
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return [rowsArray count];
}


// Customize the appearance of table view cells.
- (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];
}

// Configure the cell.
NSDictionary *dict = [rows objectAtIndex: indexPath.row];

cell.textLabel.text = [dict objectForKey:@"name"];
cell.detailTextLabel.text = [dict objectForKey:@"age"];


return cell;
}

// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
- (void)viewDidLoad {
[super viewDidLoad];
self.navigationController.navigationBar.tintColor = [UIColor colorWithRed:0.0/255.0 green:207.0/255.0 blue:255.0/255.0 alpha:1.0];
self.title = NSLocalizedString(@"NOW", @"NOW");
NSURL *url = [NSURL URLWithString:@"http://10.0.1.8/~imac/iphone/jsontest.php"];
NSString *jsonreturn = [[NSString alloc] initWithContentsOfURL:url];

//  NSLog(jsonreturn); // Look at the console and you can see what the restults are

NSData *jsonData = [jsonreturn dataUsingEncoding:NSUTF32BigEndianStringEncoding];
NSError *error = nil;

// In "real" code you should surround this with try and catch
NSDictionary * dict = [[CJSONDeserializer deserializer] deserializeAsDictionary:jsonData error:&error];
if (dict)
{
    rowsArray = [dict objectForKey:@"member"];
}

NSLog(@"Array: %@",rowsArray);
    NSLog(@"count is: %i", [self.rowsArray count]);


[jsonreturn release];
}
- (void)didReceiveMemoryWarning {
// Releases the view if it doesn't have a superview.
[super didReceiveMemoryWarning];

// Release any cached data, images, etc that aren't in use.
    }

- (void)viewDidUnload {
// Release any retained subviews of the main view.
// e.g. self.myOutlet = nil;
}


- (void)dealloc {
    [super dealloc];
    }

 @end
+1  A: 

It looks like rows is an instance variable that holds the data you want to display? If so, you're not retaining it when you assign it. Remember: if you want to keep an object, you have to claim ownership of it. The way to do this is either to allocate it yourself or retain/copy an object allocated elsewhere.

This assignment

rows = [dict objectForKey:@"member"];

doesn't do that. That means rows is getting deallocated and eventually holds a reference to a deallocated object.

Also, what's the difference between rowsArray and rows? How can you be sure that rowsArray returns the same count as rows? Generally, you should use the same data source in all of the UITableViewDataSource methods.

Alex
Wow, [rowsArray retain]; did the trick.
Michael Robinson