Question

I would have thought 'self.data=' would retain the autorelease NSMutableArray objects and the NSMutableDictionary objects it contains, but eventually I get EXC_BAD_ACCESS when the table's cellForRowAtIndexPath method tries to access the NSDictionaries in self.data.

@property (strong, nonatomic) NSMutableArray *data;

- (void) updateReceivedData:(NSData *) jsonData
{
    NSMutableArray *fetchedData = [NSJSONSerialization JSONObjectWithData:jsonData options:NSJSONReadingMutableContainers error:&error];
        self.data = [self convertDates:fetchedData withFormat:kMySQLDateTimeFormat];
        [self.tableView reloadData];
    }
}

- (NSMutableArray*) convertDates:(NSMutableArray *) array withFormat:(NSString *) format
{
    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
    [dateFormatter setDateFormat:format];
    NSMutableArray *newArray = [NSMutableArray arrayWithArray:array];
    for (NSMutableDictionary *dict in newArray)
    {
        for (id key in dict.allKeys)
        {
            if ([[dict objectForKey:key] isKindOfClass:[NSString class]])
            {
                NSString *value = [dict objectForKey:key];
                NSDate *date = [dateFormatter dateFromString:value];
                if (date) [dict setObject:date forKey:key];
            }
        }
    }
    [dateFormatter release];
    return newArray;
}

BAD_ACCESS HERE thrown here between the NSLogs.

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

   if (cell == nil) {
       NSLog (@"Cell was nil");
       cell = [[[CustomCell alloc] init] autorelease];
   }

    NSDictionary *dict = [[NSDictionary alloc] init];
    if (_isFiltered){
        dict = [self.filteredData objectAtIndex:indexPath.row];
    } else {
        dict = [self.data objectAtIndex:indexPath.row];
    }
    NSLog (@"Filling Label 1");
    cell.IDLabel.text = [[dict objectForKey:@"Id"] stringValue];
    NSLog (@"Filling Label 2");
    cell.firstNameLabel.text = [dict objectForKey:@"firstName"];
    [dict release];
    return cell;
}
Was it helpful?

Solution

Turn on zombies and see if it catches the problem (EXC_BAD_ACCESS does not necessarily mean an over-released object, but it might).

What happens to the absolute value of the retain count of an object is irrelevant.

However, a strong property implies that the object is retained, yes, if you assign through the setter (i.e. self.data = ... and not _data = ...).

OTHER TIPS

Why are you releasing the dict in cellForRowAtIndexPath: . Eventhough you allocate dict, you are assigning another pointer which is an object from filteredData or data. Just remove the [data release] and while declaring data assign it as nil

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

   if (cell == nil) {
       NSLog (@"Cell was nil");
       cell = [[[CustomCell alloc] init] autorelease];
   }

    // **As you will be assigning the object from filteredData/data, don't allocate here**
    NSDictionary *dict = nil;
    if (_isFiltered){
        dict = [self.filteredData objectAtIndex:indexPath.row];
    } else {
        dict = [self.data objectAtIndex:indexPath.row];
    }
    NSLog (@"Filling Label 1");
    cell.IDLabel.text = [[dict objectForKey:@"Id"] stringValue];
    NSLog (@"Filling Label 2");
    cell.firstNameLabel.text = [dict objectForKey:@"firstName"];
    // **Release not required as you didn't allocate**
    //[dict release];
    return cell;
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top