views:

15

answers:

1

Hi

I have this code, what am I doing wrong?

I have a function which I call playing a number of strings into an array. Then at some point I want to reload it after the user has edited the strings. This is the function:

NSMutableArray *lessonsFunc(id a, id b, id c, id d, id e, id f){
    monData *mon = [monData sharedData];
    return [NSMutableArray arrayWithObjects:@"Before School",
                                            [NSString stringWithFormat:@"%@", a],
                                            [NSString stringWithFormat:@"%@", b],
                                            @"Break",
                                            [NSString stringWithFormat:@"%@", c],
                                            [NSString stringWithFormat:@"%@", d],
                                            @"Lunch",
                                            [NSString stringWithFormat:@"%@", e],
                                            [NSString stringWithFormat:@"%@", f],
                                            @"After School", nil];
}

I call it like this:

monArrayA = lessonsFunc(mon.P11S, mon.P21S, mon.P31S, mon.P41S, mon.P51S, mon.P61S);

Then I want to reload/refresh it when I press the button:

-(IBAction)refreshLessons{
    monData *mon = [monData sharedData];
    //[monArrayA removeAllObjects];
    //[monArrayA release];
    //monArrayA = [[NSMutableArray alloc] init];
    monArrayA = lessonsFunc(mon.P11S, mon.P21S, mon.P31S, mon.P41S, mon.P51S, mon.P61S);
    //[monTable reloadData];
}

It crashes almost always when I press that button. Any help much appreciated, thanks!

+1  A: 

The likely problem is that lessonsFunc returns autoreleased array which may become invalid outside of the current scope (here - outside of refreshLessons function). Try to retain it to keep it valid as long as you need. To do that I'd suggest to declare a property for your array - compiler will automatically generate setter and getter methods for you that will handle most of memory management for you:

// header

@property (nonatomic, retain) NSMutableArray * monArrayA;

//Implementation
@synthesize monArrayA;
...
-(IBAction)refreshLessons{
    monData *mon = [monData sharedData];

    self.monArrayA = lessonsFunc(mon.P11S, mon.P21S, mon.P31S, mon.P41S, mon.P51S, mon.P61S);
}
...
- (void)dealloc{
   // Don't forget to release monArrayA in dealloc method
   [monArrayA release];
   ...
   [super dealloc];
}
Vladimir
Ahh I thought it might be something to do with that but didn't quite latch on (typical me!) thanks!!!!!
Josh Kahane
Although one small question, is there a reason why I must say self.monArrayA rather than just monArrayA?
Josh Kahane
self.monArrayA is equivalent to calling [self setMonArrayA] method (automatically generated if you used @synthesize) and monArrayA gets retained inside it. You can write monArrayA = [lessonFunc() retain], but you need to manually release previous monArrayA value. Using properties makes life much easier
Vladimir
Cool thank you. Makes sense.
Josh Kahane