squarefrog
squarefrog

Reputation: 4822

Struggling with EXC_BAD_ACCESS and UITableViewCell

I'm tearing my hair out here. My RootViewController loads an array of strings from a plist. When it loads it works fine. I then tap on a row which sends it to a new view controller. If I go back, the table still works fine, so I tap on another row which works as expected. Now if I go back and scroll I get a crash pointing at cell.textLabel.text = [self.faceCategories objectAtIndex:indexPath.row];

Abridged source code below:

// RootViewController.h

#import <UIKit/UIKit.h>

@class DetailViewController;

@interface RootViewController : UITableViewController {
    NSMutableArray *faceCategories;
}
@property (nonatomic, retain) NSMutableArray *faceCategories;
@end

RootViewController implementation // RootViewController.m

#import "RootViewController.h"

@synthesize faceCategories;

- (void)viewDidLoad
{
    [super viewDidLoad]; 

    // Init array
    if (self.faceCategories == nil) {
        NSLog(@"NIL NIL NIL NIL NIL NIL NIL NIL NIL");
        NSError *error;
        NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); //Create a list of paths.
        NSString *documentsDirectory = [paths objectAtIndex:0]; //Get a path to your documents directory from the list.
        NSString *path = [documentsDirectory stringByAppendingPathComponent:@"faceCategories.plist"]; //Create a full file path.

        NSFileManager *fileManager = [NSFileManager defaultManager];

        if (![fileManager fileExistsAtPath: path]) //Check if file exists.

        {
            NSString *bundle = [[NSBundle mainBundle] pathForResource:@"faceCategories" ofType:@"plist"]; //Get a path to your plist created before in bundle directory (by Xcode).
            [fileManager copyItemAtPath:bundle toPath: path error:&error]; //Copy this plist to your documents directory.
        }
        // Zombies percentages after
        NSMutableArray *fileContents = [[NSMutableArray alloc] initWithContentsOfFile: path]; // 55.6%
        self.faceCategories = [[NSMutableArray alloc] initWithArray:fileContents copyItems:YES]; // 33.3%
        [fileContents release]; // 11.1%

    }
}

// 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:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
    }

    /////////////////////////////
    // CRASH HERE only after I return to this view controller a couple of times
    // If I enable breakpoints and add a log I can see that faceCategories still contains the right number of objects
    /////////////////////////////
    cell.textLabel.text = [self.faceCategories objectAtIndex:indexPath.row];

    return cell;
}


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

@end

Although my memory management skills are basic, I can't see anything particularly wrong with this, so I'd appreciate a fresh set of eyes if anyone would be so kind? I find it particularly odd that it only manifests itself when I go back to the root view controller a couple of times.

Full source - http://pastebin.com/5XzwN0bA

Upvotes: 0

Views: 158

Answers (2)

squarefrog
squarefrog

Reputation: 4822

While not exactly a fix, I found that the easiest way to get rid of these errors was to enable ARC. To do this go to Edit > Refactor > Convert to Objective-C ARC.

Have a read of the various ARC blog posts, but doing this means I get to forget about memory management completely, and concentrate on new features.

Upvotes: 0

Mousa
Mousa

Reputation: 3036

Try modifying the three lines after: // Zombies percentages after with:

NSMutableArray *fileContents = [[NSMutableArray alloc] initWithContentsOfFile:path];
self.faceCategories = fileContents;
[fileContents release];

Upvotes: 1

Related Questions