choise
choise

Reputation: 25244

object crashes Application

i got an NSArray which gets filled in the init Method of my UITableViewController.

i use this object in "didSelectRowAtIndexPath" for pushing another tableviewcontroller.

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
ablogSingleCatTableViewController *singleCatTableViewController = [[ablogSingleCatTableViewController alloc] initWithStyle:UITableViewStylePlain category:[categories objectAtIndex:indexPath.row]];
[[self navigationController] pushViewController:singleCatTableViewController animated:YES];
[singleCatTableViewController release];
 }

this works a few times when i start my application. after selecting a row and getting back to the main uitableview controller at a rondom point my application crashes after selecting a row.

with some nslogs i found out, that it crashes if i try to use my "categories" object.

so

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {

NSLog(@"before");
NSLog(@"cats: %@", categories);
NSLog(@"after");

ablogSingleCatTableViewController *singleCatTableViewController = [[ablogSingleCatTableViewController alloc] initWithStyle:UITableViewStylePlain category:[categories objectAtIndex:indexPath.row]];
[[self navigationController] pushViewController:singleCatTableViewController animated:YES];
[singleCatTableViewController release];

 }

with that code my application crashes after "before" ... "after" never shows up. i dont know why my "categories" object is crashing my application ?!

my categories object is defined in my header file and has a @property (nonatomic, retain). i synthesize it and releasing it in my dealloc method.

anyone has an idea?

// edit:

some more details here, because of the comments:

Debugger Console says: "Program received signal: “EXC_BAD_ACCESS”.

i create the category array like this:

- (void)initCategories {
NSString *path = [[NSBundle mainBundle] pathForResource:@"Categories" ofType:@"plist"];
[self setCategories:[[NSArray alloc] initWithContentsOfFile:path]];
  }

calling this method in my initwithstyle method

[self initCategories];

my other custom initializing method looks something like this:

- (id)initWithStyle:(UITableViewStyle)style category:(NSDictionary*)cat {
if (self = [super initWithStyle:style]) {
    currentCategory = cat;

    items = [[NSMutableArray alloc] init];

    self.title = [currentCategory objectForKey:@"TITLE"];
    //XLog("%@", currentCategory);
}
return self;
}

Upvotes: 0

Views: 375

Answers (2)

EEE
EEE

Reputation: 4536

ok, first thing is ;

[self setCategories:[[NSArray alloc] initWithContentsOfFile:path]];

you have a leak here. just use

categories = [[NSArray alloc]  initWithContentsOfFile:path];

Crash occurs in here;

currentCategory = cat;

you have to retain, use;

currentCategory = [cat retain];

These are the problems I see in posted code, if you have not any mistake in the rest of the program, it should be fine with these fixes.

Upvotes: 1

willcodejavaforfood
willcodejavaforfood

Reputation: 44063

If you are creating your array something like this:

NSArray *tmpArray = [[NSArray alloc] initWithBlah ...];

Make sure that you assign it using the synthesized getter by using this code:

self.categories = tmpArray;
[tmpArray release];

If you do:

categories = tmpArray;
[tmpArray release];

the instance variable will not be retained at all.

Upvotes: 1

Related Questions