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.
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 asweak
as they are retained by the run loop anyway. This way, you don't need to explicitly set them tonil
after invalidating them but instead you can simply callinvalidate
every time you want to get rid of it and it will also automatically becomenil
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 callinvalidate
in case you want to cancel a timer or before replacing one, but you no longer need to set it tonil
after doing so.