Re: Trouble with NSTimer
Re: Trouble with NSTimer
- Subject: Re: Trouble with NSTimer
- From: Brian Hill <email@hidden>
- Date: Sat, 16 Jun 2001 14:52:42 -0500
On Saturday, June 16, 2001, at 02:40 PM, Joe Schiwall wrote:
Remember that if you use one of class functions, myTimer =
[NSTimer ...] to
create your timer, that is is returned to you auto-released so you need
to
retain it. I had a problem like this. What was strange was that while
the
timer was valid, it didn't get released (even though I didn't retain
it).
After I invalidated it, it was released and the next time I tried to
access
it, my program crashed.
That's probably because the run loop was still hanging onto it while the
NSTimer was valid.
So you need to do something like:
myTimer = [[NSTimer ...] retain];
// timer does it's thing here
[myTimer invalidate];
[myTimer release];
and repeat this loop every time you create a new timer.
To add a bit to this here, I've found that it's a good practice to
assign nil to released instance variables so that you don't risk
accessing freed memory.
So I'd add:
myTimer = nil;
after the [myTimer release] statement above. Then if you
('accidentally') access it again before you assign a new timer to it, it
just no ops instead of blowing up. You can also then safely check if
there's already a valid timer if (myTimer!=nil).
Brian
email@hidden
http://personalpages.tds.net/~brian_hill
"Why? I came into this game for adventure - go anywhere, travel
light, get in, get out, wherever there's trouble, a man alone.
Now they've got the whole country sectioned off and you can't
move without a form. I'm the last of a breed."
-- Archibald "Harry" Tuttle, Rogue HVAC Repairman