NSDate throwing BAD_EXCESS for what?

70 views Asked by At

I have below.

@interface MyViewController () {
    NSDate *myCurrentDate;
}

@implementation MyViewController

-(void)viewDidLoad {
    [super viewDidLoad];
    myCurrentDate = [NSDate date];
}  


- (IBAction) prevAction:(id)sender {
    NSLog(@"myCurrentDate===%@", myCurrentDate); // here it says 
    myCurrentDate = [myCurrentDate dateByAddingTimeInterval:60*60*24*-1];
    [self formatDateAndPostOnButton];
}

When I try to print current date as below, it crash saying BAD_EXCESS

NSLog(@"myCurrentDate===%@", myCurrentDate);

Below is the screenshot for the same.

enter image description here

I'm not using ARC in my project.

Any idea what is going wrong?

1

There are 1 answers

1
Grzegorz Krukowski On BEST ANSWER

Since you are not using ARC, easiest way to retain objects is to use generated setters/getters.

Instead of:

@interface MyViewController () {
    NSDate *myCurrentDate;
}

make

@interface MyViewController ()
@property(nonatomic, retain) NSDate* myCurrentDate;
@end

So it will keep NSDate retained. Right now your NSDate gets deallocated when the auto-release pool is drained.

You will need to use the getters/setters provided, however:

self.myCurrentDate = [self.myCurrentDate dateByAddingTimeInterval:60*60*24*-1];

Anyways I would recommend start using ARC to make your life simpler and avoid strange memory crashes.