views:

129

answers:

2

Hi out there.

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;
}
+1  A: 

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.

willcodejavaforfood
my way:NSString *path = [[NSBundle mainBundle] pathForResource:@"Categories" ofType:@"plist"]; categories = [[NSArray alloc] initWithContentsOfFile:path];
choise
change to self.categories and it will work. Like I did in my post.
willcodejavaforfood
it does not. hm
choise
How about [self setCategories:[[ NSArray alloc...?
Jeff Kelley
same effect. the strange thing is. i open my app, i klick on this cell, back , on , back and at a random point, it crashes... but always when i hit the cell, and everytime on the position where i try to read this array. but the 4-5-6 times before (in the same session) it worked fine.
choise
+1  A: 

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.

EEE
amazing it works now for me after retaining my "cat". im new to objective-c and new to a language where i have to retain and release objects, so im always a bit confused and don't know exactly when to do this things, especcaly retaining, releasing works quite good :Danother thing, why do i have a leak in "setCategories" ? i mean, i got categories as a property so i got a setter method for this. but thanks again! its solved.
choise
You should start with Apple's documentation, see this post http://stackoverflow.com/questions/106627/memory-management-in-objective-c , you can find useful links there. It's important to learn memory management, start with that, coding will be easier than you think. Good luck.
EEE
In "setCategories" you already increase the retain count of categories bty using @property(nonatomic, retain). But you also have [[NSArray alloc] init..], this is retained but never get released, so you will have leak. You can write [ self setCategories:[[NSAray alloc] init...] autorelease], by this way allocated nsarray will be released automatically, but it make no sense to use like that. In fact you should use only self.categories = [NSArray arrayWithContentOfFile:path]; , this will retain the count of categories and set the content.
EEE