Re: NSTimer crashing on fire
Re: NSTimer crashing on fire
- Subject: Re: NSTimer crashing on fire
- From: Tom Bernard <email@hidden>
- Date: Sat, 24 Apr 2004 18:54:47 -0600
Hi Jeremy,
Two thoughts:
* When you allocate and init your nagWindow, the window is allocated with a
retain count of 1. Do you somehow prematurely release it?
* I also wonder if you are still somehow double releasing nagTimer. Since
timers are retained by the run loop when added to the loop, you don't need
to retain or release it yourself. You are using
+ scheduledTimerWithTimeInterval:target:selector:userInfo:repeats:
which allocates, inits and autoreleases the timer. Then this method adds the
timer to the current run loop; the run loop retains the timer.
See the Apple documentation for Timers - Creating a Timer.
Regards,
Tom Bernard
email@hidden
on 4/23/04 1:04 PM, Jeremy Dronfield at email@hidden wrote:
>
I'm making some really elementary mistake here, but can't figure out
>
what it is. I've ploughed through the docs and the archives, but can't
>
find anything that answers this specific problem. The situation is
>
this: I'm using a timer to launch an Unlicensed message after a period
>
of inactivity (kind of similar to OmniWeb, but using a window). This is
>
the method called (triggered by the user making a selection in the
>
application's table view):
>
>
- (void)startNagTimer
>
{
>
if (nagTimer != nil) {
>
[nagTimer invalidate];
>
[nagTimer release];
>
}
>
>
if (nagWindow != nil) {
>
[nagWindow close];
>
[nagWindow release];
>
}
>
>
if (nagTimer == nil) {
>
nagTimer = [[NSTimer scheduledTimerWithTimeInterval:10.0 target:self
>
selector: @selector(runNagWindow:) userInfo:nil repeats:NO]
retain];
>
}
>
}
>
>
The first time through, the timer is started. On subsequent calls (user
>
has clicked the table view), the timer is invalidated and restarted. If
>
the timer is left long enough to fire, the nag window is launched.
>
Then, when the table is clicked again, the window goes away. This is
>
where it goes wrong. After it has fired and been recreated, it crashes
>
when it tries to fire again.
>
>
So, should I be looking for problems in the selector (i.e. something in
>
the method causing trouble in the run loop), or is it just that my
>
whole use of NSTimer is up the creek? In case it's relevant, all the
>
-runNagWindow: method does is create a window with a text view and some
>
text. The window is transparent and its level is NSStatusWindowLevel.
on 4/23/04 1:04 PM, Fritz Anderson at email@hidden wrote:
>
When nagTimer fires, release it and set it to nil. Non-repeating
>
NSTimers invalidate themselves when they fire. Your code issues an
>
extra invalidate to a fired nagTimer, which amounts to an extra
>
release.
on 4/23/04 4:32 PM, Jeremy Dronfield wrote:
>
I did correct the releases and setting to nil after I
>
sent in my post, but it didn't help. I now make sure the window and the
>
timer are released and set to nil after a fire. Also, instead of
>
invalidating the timer, I reset its fireDate. Problem persists. I've
>
discovered that it is in fact my selector method that's causing the
>
crash, though I still don't know why. I tried commenting out all the
>
code in my -runNagWindow: method except for an NSLog(). Result: no
>
crash. The timer resets, fires, resets, resets, resets, fires... etc. I
>
then tried commenting out bits of the method, and got it down to the
>
allocation of the window. This is it:
>
>
if (nagWindow == nil) {
>
nagWindow = [[TransparentWindow alloc] initWithContentRect:nagRect
>
styleMask:
>
NSBorderlessWindowMask backing:NSBackingStoreBuffered defer:NO];
>
}
>
>
I've tried changing the backing to non-retained, but it didn't help. I
>
also tried using a standard NSWindow instead of the TransparentWindow
>
subclass, and that didn't help either.
>
>
Any idea what's going wrong here?
_______________________________________________
cocoa-dev mailing list | email@hidden
Help/Unsubscribe/Archives:
http://www.lists.apple.com/mailman/listinfo/cocoa-dev
Do not post admin requests to the list. They will be ignored.