2

Possible Duplicate:
NSTimer doesn't stop

I am using a NSTimer to update the value of a slider while a audio is playing.

 if (sliderUpdateTimer) {
    [sliderUpdateTimer invalidate];
    sliderUpdateTimer=nil;
}

sliderUpdateTimer = [NSTimer scheduledTimerWithTimeInterval:0.1 target:self selector:@selector(updateSliderForAudio) userInfo:nil repeats:YES];

once the audio is finished playing i am invalidating the Timer in audioPlayer delegate method.

-(void)audioPlayerDidFinishPlaying:(AVAudioPlayer *)player successfully:(BOOL)flag{
NSLog(@"audioPlayerDidFinishPlaying");
[player release];
audioPlayer=nil;

[sliderUpdateTimer invalidate];
[sliderUpdateTimer release];
sliderUpdateTimer=nil;

}

The delegate method is getting called but the timer is not stopping.... I have only once thread on which i am runing the timer. But still the timer does not stop.... Any help in this regard is welcome...

Community
  • 1
  • 1
A for Alpha
  • 2,904
  • 8
  • 42
  • 76
  • That is the correct way of stopping a timer, there must be something else wrong. What are you seeing? Is the function `updateSliderForAudio` still being called? How can you tell? Is the app crashing? – Thomas Clayson Feb 02 '12 at 09:11
  • which method contains the `if (sliderUpdateTimer) { ...` code??? – Inder Kumar Rathore Feb 02 '12 at 09:12
  • The app is not getting crashed. But, the updateSliderForAudio is being called continuously even after the timer is invalidated.... The log statements in the method are getting called even after invalidating the method – A for Alpha Feb 02 '12 at 09:17

3 Answers3

6

First of all, you would be over releasing it and should receive EXC_BADACCESS (you're not retaining the timer when you set it, so you shouldn't release it either). Should only call:

[sliderUpdateTimer invalidate];
sliderUpdateTimer=nil;

Since you don't receive a crash, it seems sliderUpdateTimer is nil when calling audioPlayerDidFinishPlaying:. User NSLog or put a breakpoint to check if this is true. If this is the case, you're probably setting it to nil at some point, search for slideUpdateTimer and check where this might occur.

alex-i
  • 5,406
  • 2
  • 36
  • 56
4

Hey try this thing...

- (void)stopTimer{
    [sliderUpdateTimer invalidate];
    //don't release it. as it is autoreleased.
    //[sliderUpdateTimer release];
    sliderUpdateTimer=nil;
}


-(void)audioPlayerDidFinishPlaying:(AVAudioPlayer *)player successfully:(BOOL)flag{
    NSLog(@"audioPlayerDidFinishPlaying");
    [player release];
    audioPlayer=nil;
    [self performSelectorOnMainThread:@selector(stopTimer) withObject:nil waitUntilDone:NO];
}

I know you have only single thread but there may be two different run loops

Inder Kumar Rathore
  • 39,458
  • 17
  • 135
  • 184
2

Try after removing the below lines from 1st section of your code

if (sliderUpdateTimer){
    [sliderUpdateTimer invalidate];
    sliderUpdateTimer=nil;
}

Don't need to call release message on that sliderUpdateTimer object

Because you have created that object with pending autorelease message

Here no need to use these lines.

I hope this will work.

Kamar Shad
  • 6,089
  • 1
  • 29
  • 56