Re: NSTimer crashes after invalidation
Re: NSTimer crashes after invalidation
- Subject: Re: NSTimer crashes after invalidation
- From: Shawn Erickson <email@hidden>
- Date: Mon, 14 Feb 2005 16:46:50 -0800
On Feb 14, 2005, at 4:43 PM, Michael Becker wrote:
Hi!
Here's the deal: I have an instance variable of type NSTimer. In an
instance method, I set up the timer via +scheduledTimer:... to fire
after 1 sec. So the instance variable should hold a reference to that
timer.
Now the method that sets up the timer can be called frequently within
a short amount of time, and I want to invalidate the timer if it
hasn't fired yet, and then set it up again. So the timer will only
fire once if the user gives it enough time, instead of firing once for
each user click, no matter how fast the user clicks.
Here is the code in my method that sets up the timer (it's in a
subclass of NSScrollView, if you care):
if (repaintTimer != nil) {
// This line crashes
[repaintTimer invalidate];
}
repaintTimer = [NSTimer scheduledTimerWithTimeInterval:1.0f
target:[self documentView] selector:@selector(noLiveZoom) userInfo:nil
repeats:NO];
But it seems that if I call that method after the timer has once
fired, the indicated line crashes. The check for "repaintTimer != nil"
will ALWAYS give a YES, telling me that the repaintTimer variable is
"something" but not nil and obviously not an NSTimer, as sending any
kind of message to it will result in a crash.
What happens to my repaintTimer instance variable after the timer has
fired? How can I procrastinate the timer's execution in a correct way?
You are not retaining the timer yet you are using a method that returns
you an auto-released object. Also the timer isn't set to repeat so it
is only retained by the run loop until after the timers call back has
been called and returned from.
So retain the timer if needed but release it when the timer call back
fires to insure you don't leak and/or change your logic so that
repainTimer is properly set to nil.
Finally note that sending a message to nil is a no op so you test for
nil in the above is not needed.
-Shawn
_______________________________________________
Do not post admin requests to the list. They will be ignored.
Cocoa-dev mailing list (email@hidden)
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden