0

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;
}
choise
  • 24,636
  • 19
  • 75
  • 131
  • Can you provide more details on the crash? If you debug the application rather than just running it (cmd-Y vs. cmd-R), you'll usually have the debugger pause execution at the point of the crash. That way you can examine the exact backtrace and see the values of any instance variables at the time. – Noah Witherspoon Feb 13 '10 at 19:59
  • Show the code where you create the categories array please – willcodejavaforfood Feb 13 '10 at 19:59
  • I think the you have a problem in ablogSingleCatTableViewController initWithStyle:category: method. Can you post your code? – EEE Feb 13 '10 at 20:55
  • i edited my first post. see if it help =( – choise Feb 13 '10 at 22:48

2 Answers2

1

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
  • 43,223
  • 17
  • 81
  • 111
  • my way: NSString *path = [[NSBundle mainBundle] pathForResource:@"Categories" ofType:@"plist"]; categories = [[NSArray alloc] initWithContentsOfFile:path]; – choise Feb 13 '10 at 20:06
  • change to self.categories and it will work. Like I did in my post. – willcodejavaforfood Feb 13 '10 at 20:39
  • How about [self setCategories:[[ NSArray alloc...? – Jeff Kelley Feb 13 '10 at 21:48
  • 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 Feb 13 '10 at 21:57
1

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
  • 4,536
  • 3
  • 28
  • 34
  • 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 :D another 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 Feb 14 '10 at 00:20
  • 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 Feb 14 '10 at 00:27
  • 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 Feb 14 '10 at 00:34