Hii all,
i am writting the following code but my application is crashing . I don no why.Actually i want to calculate the speed but app crashes at line no 4 .please explain.debugger says..
Program received signal: “EXC_BAD_ACCESS”.
- (void)viewDidLoad {
[super viewDidLoad];
NSDate *date = [NSDate date];
initialDate = date;
}
-(void) showSpeed
{
[self.initialDate retain];
NSDate *endDate = [NSDate date];
NSTimeInterval interval = [endDate timeIntervalSinceDate:self.initialDate];
//NSLog(@"time %f",interval);
double speed ;
speed = distance/interval;
NSString *speedValue = [NSString stringWithFormat:@"%1.2f",speed];
showResult.text = speedValue;
[self.initialDate release];
}
You probably didn’t retain initialDate when you stored it.
[NSDate date]returns an autoreleased object, and you need to retain it or use a retaining property.Edit after viewDidLoad code is available:
You need to retain the object that
[NSDate date]gives you, because it is autoreleased.No reason to retain/release it in showSpeed though.
I cannot recommend Apple’s Memory Management Guide enough – it really is essential and worth reading more than once. 🙂