Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

UITableView crashes when scrolling

I've been attempting to create my own app for a band by my friends, and I've been experimenting with using a custom TableViewCell for news articles that appear on the website. My main objective is to get all of the data from the website, store it in a NSMutableArray, and then display that in my custom cells.

The app runs fine when it loads the first 5-6 cells. However, when I begin to scroll, the app crashes. I've pinpointed the in the cellForRowAtIndexPath: method. Using GDB, I've also come to find out that after I create my NSMutableArray data and once the program runs, after scrolling, my array seems to be autoreleased. I'm not sure why this happens. Here's what I have for my code thus far:

In HomeViewController.h:

@interface HomeViewController : UIViewController {
    NSArray *results;
    NSMutableArray *titles;
    NSMutableArray *dates;
    NSMutableArray *entries;
}

@property (nonatomic, retain) NSMutableArray *titles;
@property (nonatomic, retain) NSMutableArray *dates;
@property (nonatomic, retain) NSMutableArray *entries;

@end

In HomeViewController.m:

- (void)viewDidLoad {
[super viewDidLoad];
titles = [[NSMutableArray alloc] init];
dates = [[NSMutableArray alloc] init];
entries = [[NSMutableArray alloc] init];

while((i+1) != endIndex){   
    NSString *curr_title = [[NSString alloc] init]; 
    NSString *curr_date = [[NSString alloc] init];
    NSString *curr_entry = [[NSString alloc] init];

    //do some character iterations across a string

    [titles addObject:curr_title];
    [dates addObject:curr_date];
    [entries addObject:curr_entry];

    [curr_title release];
    [curr_date release];
    [curr_entry release];
}
}

//more code here, removed

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

static NSString *CellIdentifier = @"NewsCell";


NewsCell *cell = (NewsCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];

if (cell == nil) {
    NSArray *topLevelObjects = [[NSBundle mainBundle]
                                loadNibNamed:@"NewsCell"
                                owner:self options:nil];
//  cell = [topLevelObjects objectAtIndex:0];
    for(id currentObject in topLevelObjects){
        if([currentObject isKindOfClass:[UITableViewCell class]]){
            cell = (NewsCell *)currentObject;
            break;
        }
    }

}
NSLog(@"%d", indexPath.row);
NSLog(@"%d", titles.count);
cell.cellTitle.text = [titles objectAtIndex:indexPath.row];
cell.datePosted.text = [dates objectAtIndex:indexPath.row];
cell.preview.text = [entries objectAtIndex:indexPath.row];

return cell;
}

Again, the first 5-6 cells show up. Once I scroll, I tried doing po titles and got this error:

Program received signal EXC_BAD_ACCESS, Could not access memory. Reason: KERN_PROTECTION_FAILURE at address: 0x00000009 0x011b309b in objc_msgSend ()

I've tried allocating the arrays in initWithNibName: but that didn't seem to do much. I've tried moving all the code in viewDidLoad: and then calling [super viewDidLoad] and it just produced the same results as well. Sorry this is so long, but I figured people needed to see my code.

like image 499
SpacePyro Avatar asked Feb 09 '26 16:02

SpacePyro


1 Answers

I don't see anything obviously wrong with the code you posted. Try enabling the NSZombieEnabled environment variable. This prevents objects from being released so that when your application "crashes" you can determine which object caused the problem.

Also, instead of looping through array of objects returned by loadNibNamed:owner:options, you should assign the desired object to an IBOutlet property of your class. See Loading Custom Table-View Cells From Nib Files for an example.


The following is extracted from the new code you posted:

NSString *curr_title = [[NSString alloc] init];

//do some character iterations across a string

[titles addObject:curr_title];

[curr_title release];

NSString is not mutable (as is NSMutableString). Are you intentionally adding empty strings to the titles array?


In response to your comment: stringByAppendingString creates a new autoreleased NSString object.

NSString *curr_title = [[NSString alloc] init];

// this leaks the original NSString object (curr_title no longer points to it);
// curr_title now points to a new, autoreleased NSString
curr_title = [curr_title stringByAppendingString:@"..."];

[titles addObject:curr_title];

// releasing the autoreleased NSString will cause your application to crash!
[curr_title release];
like image 198
titaniumdecoy Avatar answered Feb 12 '26 15:02

titaniumdecoy



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!