1

I have an NSTimer declared in my .h and in the viewDidLoad of the /m I have the code:

timer = [NSTimer scheduledTimerWithTimeInterval:kComplexTimer target:self selector:@selector (main) userInfo:nil repeats:YES];

I also have [timer release]; in my dealloc.

However when I exit the view and return to it, the timer has not in fact released, it has doubles in speed! How do I solve this & what am I doing wrong???

Thanks

Conor Taylor
  • 2,998
  • 7
  • 37
  • 69

4 Answers4

6

you don't need to release it as you have not retained it - as a rule. all you need to do is just call [timer invalidate]; which will stop your timer.

Saurabh Passolia
  • 8,099
  • 1
  • 26
  • 41
3

Nice Answer , but good to check whether the time is nil or not to avoid unwanted exception..

if( timer ! = nil )
{
  [timer invalidate];
  timer = nil;
}

Thank you...

jillu_
  • 132
  • 1
  • 1
  • 6
2
[timer invalidate];
timer = nil;

The second line is important if you want to reset the NSTimer

Fabio Poloni
  • 8,219
  • 5
  • 44
  • 74
  • +1 The `nil` is very key. I used to have issues with timers and their reuse until adding that part. – dredful Dec 17 '11 at 17:13
1

You must not call release on a object that it not be created by "new", "alloc", "retain", "copy".

In this case, you had created a Timer by scheduledTimerWithTimeInterval method, So you must not call release method but call [timer invalidate] to stop the timer.

virushuo
  • 660
  • 3
  • 5