I'm trying to create an NSTimer, then invalidate and release it, then set it to a new timer. However, I am getting an EXC_BAD_ACCESS when trying to set the timer member var again.
Code below:
1) I set the timer member var (it's set to retain)
self.mPageTimer = [NSTimer scheduledTimerWithTimeInterval:kPageTimerInterval target:self selector:@selector(pageTimerCallback) userInfo:nil repeats:NO];
2) I let it go
[mPageTimer invalidate];
[mPageTimer release];
This results in the crash when I try to call the snippet in step 1 again, but I am not sure why. I retained it by setting it, and then I release it, so shouldn't the object be taken care of and my member var ok to set to a new allocated timer?
If I do this, it doesn't crash and works fine:
[mPageTimer invalidate];
[mPageTimer release];
mPageTimer = nil;
I can't see how I'm doing something wrong with releasing the object, because, regardless of if that were the case, shouldn't I be able to always set my member var to whatever newly created nstimer, leak or not?
Is it safe to assume, that...
@property
, but from another method while you@synthesize mPageTimer
and@property (nonatomic, retain) NSTimer* mPageTimer
?(whether nonatomic or atomic doesn't matter)
If that's the case, your crash is expected:
So in short:
If you have a property for a timer, provide your own setter and make every access use it.
(Two exceptions allowed: 1. implementation of the setter 2. in dealloc where you
release
the timer*)(* Don't invalidate a timer that you're the target of in dealloc: It is absolutely pointless!)