views:

33

answers:

1

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];


   }
+1  A: 

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.

- (void)viewDidLoad {
     [super viewDidLoad];

     initialDate = [[NSDate date] retain];
}

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. :-)

Eiko
i have retained it using [self.initialDate retain]; but still it is crashing
Siddharth
Please show this code. - Ideally editing your question.
Eiko
i have edited the code.....
Siddharth
You need to retain it when you set it, without seeing this code it's just guessing. So without seeing your viewDidLoad method and the full showSpeed method it is very hard to figure out what's wrong. If you post any message you get when crashing (debug mode gives more info), this will be helpful.
Eiko
i have posted the viewDidLoad method please have a look....
Siddharth
Edited with new code ;-)
Eiko
hey Eiko problem is solved ...you were right i retained the date when i was setting it now my code is working fine...thank you very much!
Siddharth