views:

459

answers:

1

I've been stuck on this for days and each time I come back to it I keep making my code more and more confusing to myself, lol. Here's what I'm trying to do. I have table list of charges, I tap on one and brings up a model view with charge details. Now when the model is presented a object is created to fetch a XML list of users and parses it and returns a NSMutableArray via a custom delegate. I then have a button that presents a picker popover, when the popover view is called the user array is used in an initWithArray call to the popover view. I know the data in the array is right, but when [pickerUsers count] is called I get an EXC_BAD_ACCESS. I assume it's a memory/ownership issue but nothing seems to help. Any help would be appreciated.

Relevant code snippets:

Charge Popover (Charge details model view):

@interface ChargePopoverViewController .....
NSMutableArray *pickerUserList;
@property (nonatomic, retain) NSMutableArray *pickerUserList;

@implementation ChargePopoverViewController
@synthesize whoOwesPickerButton, pickerUserList;
- (void)viewDidLoad {   
    JEHWebAPIPickerUsers *fetcher = [[JEHWebAPIPickerUsers alloc] init];
    fetcher.delegate = self;
    [fetcher fetchUsers];
}
-(void) JEHWebAPIFetchedUsers:(NSMutableArray *)theData {
    [pickerUserList release];
    pickerUserList = theData;
}
- (void) pickWhoPaid: (id) sender {
    UserPickerViewController* content = [[UserPickerViewController alloc] initWithArray:pickerUserList];
    UIPopoverController *popover = [[UIPopoverController alloc] initWithContentViewController:content];
    [popover presentPopoverFromRect:whoPaidPickerButton.frame inView:self.view permittedArrowDirections:UIPopoverArrowDirectionAny animated:YES];
    content.delegate = self;    
}

User Picker View Controller

@interface UserPickerViewController .....
NSMutableArray *pickerUsers;
@property(nonatomic, retain) NSMutableArray *pickerUsers;

@implementation UserPickerViewController
@synthesize pickerUsers;
-(UserPickerViewController*) initWithArray:(NSMutableArray *)theUsers {
    self = [super init];

    if ( self ) {
        self.pickerUsers = theUsers;
    }

    return self;
}
- (NSInteger)pickerView:(UIPickerView *)thePickerView numberOfRowsInComponent:(NSInteger)component {
    // Dies Here EXC_BAD_ACCESS, but NSLog(@"The content of array is%@",pickerUsers); shows correct array data 
    return [pickerUsers count];
}

I can provide additional code if it might help. Thanks in advance.

+2  A: 

You declare the ivar holding the array as this...

@property (nonatomic, retain) NSMutableArray *pickerUserList;

But then you have a method implemented like this:

-(void) JEHWebAPIFetchedUsers:(NSMutableArray *)theData {
    [pickerUserList release];
    pickerUserList = theData;
}

You aren't retaining theData and you aren't calling the synthesized setter. If you did Build and Analyze, it should catch this problem and tell you about it. If not, file a bug.

bbum
Would that be accomplished by: [theData retain]; self.pickerUserList = theData;
JoshEH
First, go read the Cocoa Memory Management Guide. It should be clear from that. Next, Build `
bbum