tags:

views:

112

answers:

2

Hello All! I have a (hopefully silly) iPhone programming question. I'm trying to get a string out of an object I created, stored in an AppDelegates NSMutableArray--is there a reason I always get an EXC_BAD_ACCESS error when I try to access (via the appdelegates array, or my controllers copy of it) my NSMutableArray in the cellForRowAtIndexPath method, but NO PROBLEM whatsoever in my ViewDidLoad method? Any thoughts appreciated!

In my "viewDidLoad" method, I have the following code:

    appDelegate = (Social_Cost_TrackerAppDelegate *)[[UIApplication sharedApplication] delegate];
    [appDelegate setArray];
    myCompanies = appDelegate.companies;
    NSLog(@"Important Value: %i",[myCompanies count]);
    Company *c = [myCompanies objectAtIndex:0];
    NSString *me = [c cname];
    NSLog(@"2nd Val: %@", me);

In my "cellForRowAtIndexPath" I have the following code:

    NSLog(@"Problem Spot--myCompanies-Size: %i",[myCompanies count]);
    Company *c = [myCompanies objectAtIndex:0];
    NSString *me = [c cname];
    NSLog(@"2nd Val: %@", me);

Yet, in the "cellForRowAtIndexPath" method, I get the following error:

Program received signal: “EXC_BAD_ACCESS”.

What am I doing wrong, why can I get a size out of my array, but not an object? I'm a bit stumped here... Thanks for any thoughts! (P.S. I'm not an iPhone/Objective-C developer--just doing a bit of learning here with a side-project).

Below is my debugger output: 2009-11-19 18:42:57.883 Social Cost Tracker[688:207] Important Value: 5 2009-11-19 18:42:57.883 Social Cost Tracker[688:207] 2nd Val: UCBrewers 2009-11-19 18:42:57.885 Social Cost Tracker[688:207] Problem Spot--myCompanies-Size: 5 Program received signal: “EXC_BAD_ACCESS”.

A: 

You are not retaining your myCompanies object when you first set it, so it won't be available in your cellForRowAtIndexPath. You need to either declare the myCompanies array in your header as a property like this:

@property (retain) NSArray* myCompanies;

and then @sythesize it in the .m with

@synthesize myCompanies;

Or you need to explicitly call retain in your code like this:

appDelegate = (Social_Cost_TrackerAppDelegate *)[[UIApplication sharedApplication] delegate];
[appDelegate setArray];
myCompanies = [[appDelegate companies] retain];
// ... etc.

In which case you will also need to call release when you're done with the array, most likely in the view controller's dealloc.

Matt Long
Thanks Matt,I do have my property set up as follows, and am synthesizing myCompanies as well.@property (nonatomic, retain) NSMutableArray *myCompanies;I tried it with just "retain" to see if that somehow worked magic, but so far no luck :( I also tried it with retaining in the .m file as you described, but still get that darn exc_bad_access message. Any other thoughts?? Thanks!
sigmyers
Which line is it failing on in cellForRowAtIndexPath?. What do you get when you print the entire array contents with NSLog(@"Contents: %@", myCompanies); in cellForRowAtIndexPath?
Matt Long
A: 

what the setArray method doing? are you sure that your viewDidLoad is called before cellForRowAtIndexPath? you can try to call setArray in your appDelegate class in applicationDidFinishLaunching. or in init method of your class

Morion