Curnelious
Curnelious

Reputation: 1

timeIntervalSinceDate inside an if statement

my code is crashing because of this problem:

if ([recorder peakPowerForChannel:0]==0 )
{       
    NSLog(@"we are recording now because signal is at high volume... ");
    if(flag==1)
    {
        start = [NSDate date];       
    }
    [recorder stop];            
}
else 
{
    [recorder stop];    
    stop = [NSDate date];
    duration = [start timeIntervalSinceDate:stop];

stop and start are NSDate and declared at the start of the .m ,The problem is that because "duration" and "start" are not in the same "if", and moving "start" to be under the "else" solve it. but i need it to be there.

why is that happen? the software is going for sure to the "start" before she goes to the stop, so does he forget the start?

Upvotes: 0

Views: 214

Answers (2)

dasdom
dasdom

Reputation: 14073

The [NSDate date] gives a autoreleased object to the caller. Change start = [NSDate date]; to start = [[NSDate date] retain]; and you should be fine.

Then you have to balance the retain counts in the end.

Upvotes: 2

Claus Broch
Claus Broch

Reputation: 9212

Move initialization of start before the if statement. That way it will always be valid.

Upvotes: 0

Related Questions