Crash when checking if NSTimer isValid? - ios

I am getting this exception in the console:
Error:
2015-06-25 23:12:01.841 Copyfeed for Mac[9512:584232]
-[_NSViewLayoutAux invalidate]: unrecognized selector sent to instance 0x6000001657c0
when checking if my timers are valid/and when invalidating them.
if ([_staticTimer isValid]) {
[_staticTimer invalidate];
_selectionTimer =
[NSTimer scheduledTimerWithTimeInterval:2 target:self
selector:#selector(hideHUD) userInfo:nil repeats:NO];
}
if ([_selectionTimer isValid]) {
[_selectionTimer invalidate];
_selectionTimer =
[NSTimer scheduledTimerWithTimeInterval:2 target:self
selector:#selector(hideHUD) userInfo:nil repeats:NO];
}
This is my new code:
if (_selectionTimer != nil) {
[_selectionTimer invalidate];
_selectionTimer = nil;
_selectionTimer = [NSTimer scheduledTimerWithTimeInterval:2 target:self selector:#selector(hideHUD) userInfo:nil repeats:NO];
}
if (_staticTimer != nil) {
[_staticTimer invalidate];
_staticTimer = nil;
_selectionTimer = [NSTimer scheduledTimerWithTimeInterval:2 target:self selector:#selector(hideHUD) userInfo:nil repeats:NO];
}
#property (strong )NSTimer *staticTimer;
#property (strong )NSTimer *selectionTimer;
Now getting this error when I debug with zombie objects on.
2015-06-26 00:39:45.523 Copyfeed for Mac[11191:824502] ***
-[CFRunLoopTimer release]: message sent to deallocated instance 0x608000175e40

There are a couple of potential issues here.
if (_staticTimer != nil) {
[_staticTimer invalidate];
_staticTimer = nil;
_selectionTimer = [NSTimer scheduledTimerWithTimeInterval:2 target:self selector:#selector(hideHUD) userInfo:nil repeats:NO];
}
What you are doing here is overriding the selectionTimer even though it might still contain a timer that is still scheduled in the run loop. So if you reset the property here, you should also make sure to call [_selectionTimer invalidate] before doing so.
Depending on what you are doing when the timer is firing, this could explain the crash on CFRunLoopTimer.
A general advice that turned out to be very helpful for me when working with NSTimer: I would recommend declaring all the properties that hold a scheduled timer as weak as they are retained by the run loop anyway. This way, you don't need to explicitly set them to nil after invalidating them but instead you can simply call invalidate every time you want to get rid of it and it will also automatically become nil once it has fired by the run loop, releasing all the data it might hold on to.
Note that this would still require you to call invalidate in case you want to cancel a timer or before replacing one, but you no longer need to set it to nil after doing so.

After you invalidate it, you should set an NSTimer object to nil.
invalidate method also does a release.
If you didn't do that, calling a method on it like isValid could cause your crash.
if (_selectionTimer != nil) {
[_selectionTimer invalidate];
_selectionTimer = nil;
// do something
}
Check here
if (_staticTimer != nil) {
[_staticTimer invalidate];
_staticTimer = nil;
//_selectionTimer = [NSTimer scheduledTimerWithTimeInterval:2 target:self selector:#selector(hideHUD) userInfo:nil repeats:NO];
_staticTimer = [NSTimer scheduledTimerWithTimeInterval:2 target:self selector:#selector(hideHUD) userInfo:nil repeats:NO];
}

From the documentation:
Because the run loop maintains the timer, from the perspective of
memory management there's typically no need to keep a reference to a
timer after you’ve scheduled it. Since the timer is passed as an
argument when you specify its method as a selector, you can invalidate
a repeating timer when appropriate within that method. In many
situations, however, you also want the option of invalidating the
timer—perhaps even before it starts. In this case, you do need to keep
a reference to the timer, so that you can send it an invalidate
message whenever appropriate. If you create an unscheduled timer (see
“Unscheduled Timers”), then you must maintain a strong reference to
the timer (in a reference-counted environment, you retain it) so that
it is not deallocated before you use it.
So you should make the timer weak instead of strong

Related

How to stop an NSTimer?

I have an NSTimer that I want to be stopped when I leave my vViewVontroller:
The timer is in a method that I call from viewWillAppear :
- (void) myMehtod
{
//timer = [[NSTimer alloc] init];
// appel de la methode chaque 10 secondes.
timer = [NSTimer scheduledTimerWithTimeInterval:10.0f
target:self selector:#selector(AnotherMethod) userInfo:nil repeats:YES];
//self.timerUsed = timer;
}
I call the method stopTimer in viewWillDisappear
- (void) stopTimer
{
[timer invalidate];
timer = nil;
}
PS: I tried the answer of user1045302 in this question but it didn't work:
How to stop NSTimer
The source of the problem probably is that myMehtod is called twice or more times.
Since the method does not invalidate existing timers before setting up the new one you actually have several timers ticking at the same time.
Fix is easy: invalidate old timers before setting up a new one:
- (void)myMehtod
{
[timer invalidate];
timer = [NSTimer scheduledTimerWithTimeInterval:10.0f
target:self
selector:#selector(anotherMethod)
userInfo:nil
repeats:YES];
}

Multiple timers set un in the same variable

Is this code a problem?
self.UpdateTimer = [NSTimer scheduledTimerWithTimeInterval:60. target:self selector:#selector(update:) userInfo:nil repeats:YES];
I notice that this is in a method that is called often. However there is only one place where the timer is invalidated and set to nil ... the dealloc of that class.
Does setting a new timer multiple times automatically invalidate the old timer?
No, setting the timer to a new object does not invalidate the previous timer. You will end up with multiple timers running if you call that line of code multiple times, but only invalidate the currently referenced timer in one place elsewhere.
When you create the timer, you should probably check if one is already set and invalidate it before creating a new one.
if( self.UpdateTimer )
{
[self.UpdateTimer invalidate];
self.UpdateTimer = nil;
}
self.UpdateTimer = [NSTimer scheduledTimerWithTimeInterval:60. target:self selector:#selector(update:) userInfo:nil repeats:YES];
OR, simply leave the original one running, if you don't need to reset the timer interval.
if( !self.UpdateTimer )
{
self.UpdateTimer = [NSTimer scheduledTimerWithTimeInterval:60. target:self selector:#selector(update:) userInfo:nil repeats:YES];
}

Unable to stop NSTimer

I use this code for stopping NSTimer
[timer invalidate]
timer = nil;
It works fine for the first run. But, after I resume the timer with this code.
timer = [NSTimer scheduledTimerWithTimeInterval:5.0
target:self
selector:#selector(checkNewUpdates)
userInfo:nil
repeats:YES];
NSTimer won't stop anymore with [timer invalidate]
It look like multiple instance of timer is running simultaneously. You can do one thing, before start to run a new timer, check for previous instance of timer, and if timer instance is available, then invalidate it. After this start new instance
if(timer)
{
[timer invalidate];
timer = nil;
}
timer = [NSTimer scheduledTimerWithTimeInterval:5.0
target:self
selector:#selector(checkNewUpdates)
userInfo:nil
repeats:YES];
In apple's official document they said:
You must send this message from the thread on which the timer was
installed. If you send this message from another thread, the input
source associated with the timer may not be removed from its run loop,
which could prevent the thread from exiting properly.
If your timer is running on main thread, do this:
[timer performSelectorOnMainThread:#selector(invalidate) withObject:nil waitUntilDone:YES];
If it is on any other thread, lets call the thread myThread, then do this:
[timer performSelector:#selector(invalidate) onThread:myThread withObject:nil waitUntilDone:NO];
Hope this helps.. :)
Just invalidate the timer inside the selector that fires. That will ensure you have a pointer to the correct timer (which is probably why your invalidate call isn't working:
timer = [NSTimer scheduledTimerWithTimeInterval:5.0
target:self
selector:#selector(checkNewUpdates:)
userInfo:nil
repeats:YES];
Note the colon after checkNewUpdates:
Then, in your checkNewUpdates: method, do something like this:
- (void)checkNewUpdates:(NSTimer*)timer
{
// do somehting
// Then, check if the criteria for stopping the timer has been met, and invalidate it here.
if( self.shouldStopTimer ) // made up variable, use your own criteria.
{
[timer invalidate];
}
}
I know this doesnt answer your question per-se;
Can I suggest using polling mechanism instead of a timer? Ive had a world of trouble with NSTimers in the past and polling was a good alternative. Ive made a simple one below.
- (void) doPoll {
// do logic here
if (shoudStop) {
return;
}
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, X * NSEC_PER_SEC)),
dispatch_get_main_queue(), ^{
[self doPoll];
});
}
This is just a simple example, it does not stop retain cycles If you choose to try this, Yours should.
Hope it helps.

How do I see if NSTimers are stacking and how to prevent it?

Im making an iphone game that uses NStimer for movement. I found out that there is a bug that makes the timers stack some times, but i havnt found out what causes is. Is there any way to see how many timers are allocated and is there any way to prevent it by doing something like this:
If(myTimer.numberOfAllocatedTimers == 0) {
myTimer = [NSTimer scheduledTimerWithTimeInterval:0.25 target:self selector:#selector(updateme) userInfo:nil repeats:YES];
}
Try this:
// use this to start timer
myTimer = [NSTimer scheduledTimerWithTimeInterval:1 target:self selector:#selector(doSthing) userInfo:nil repeats:YES];
// use this to stop timer
if (myTimer) {
[myTimer invalidate];
myTimer = nil;
}
A little more context on where this code is being used would be helpful.
However, I'll try to explain why it's happening anyway.
Every time you use [NSTimer scheduledTimerWithTimeInterval...], a new timer is created, and will start repeating. If your code creates a timer inside the callback method, see example below, you'll stack timers.
- (void)updateme {
if ([NSTimer scheduledTimerWithTimeInterval:0.25 target:self selector:#selector(updateme) userInfo:nil repeats:YES]) {
// This creates stacked timers!
}
}
Here's how you should do it:
#property (nonatomic, strong) NSTimer *myTimer;
...
self.myTimer = [NSTimer scheduledTimerWithTimeInterval:0.25 target:self selector:#selector(updateme:) userInfo:nil repeats:YES];
...
- (void)updateme:(NSTimer *)timer {
if (timer == self.myTimer) {
// Do something because the timers are equal
}
}

iOS Timer loop that executes a certain action every X minutes

I am trying to execute a certain block of code every x amount of time, but it seems that all I am doing is executing it during that time. Here's a block of my code.
while (TRUE) {
NSTimer *countDown = [NSTimer
scheduledTimerWithTimeInterval:(x)
target:self
selector:#selector(timerHandle)
userInfo:nil
repeats:YES];
}
Any ideas as to how to do it?
As written, this is an infinite loop, creating an NSTimer every loop iteration.
Try it without the while loop. This should cause [self timerHandle] to be invoked on interval x by a single background thread/timer. The Apple guide to NSTimer usage (including as others point out, how to properly stop your timed task) is here.
Try this: (It will call executeMethod on every 5 sec)
if (![NSThread isMainThread]) {
dispatch_async(dispatch_get_main_queue(), ^{
[NSTimer scheduledTimerWithTimeInterval:5.0
target:self
selector:#selector(executeMethod)
userInfo:nil
repeats:YES];
});
}
else{
[NSTimer scheduledTimerWithTimeInterval:5.0
target:self
selector:#selector(executeMethod)
userInfo:nil
repeats:YES];
}
Write the code you want to be executed in executeMethod method. Hope this helps.. :)

Resources