Man of One Way
Man of One Way

Reputation: 3980

NSTimer auto-invalidation

How long does it take before the NSTimer auto-invalidates itself when using a timer with repeats:NO. Could this be a false alert when running the profiler for leaking?

When I have made a server request, I get a success or a failure. Either way I create a new timer

_timer = [NSTimer scheduledTimerWithTimeInterval:3.0 target:self selector:@selector(updateSystems) userInfo:nil repeats:NO];

When the timer fires, the application makes a new server request.

Upvotes: 1

Views: 515

Answers (2)

Ilanchezhian
Ilanchezhian

Reputation: 17478

The timer will be invalidated after it fires (after the time interval u have given).

See ref.

Also, inside the selector that is invocated by the timer, the code block should be covered with NSAutoreleasePool.

The selector method should be like this.

-(void)timerMethod:(NSTimer*)timer
{
  NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];

  ....
  [pool drain];
}

Upvotes: 0

Pripyat
Pripyat

Reputation: 2937

A timer will invalidate after the time interval you set has been reached and it has no further loops to execute. So if you set repeats:NO, it will invalidate itself.

I think that the leaks aren't in NSTimer, but in the selector it executes instead, which is handled by you. Use Xcode's Analyzer Tool (CMD-Shift-A) to find where the leaks are and/or check that you release whatever you alloc & retain in that selector.

NSTimer is innocent. :)

Upvotes: 2

Related Questions