Re: Unjustified CPU load with a simple Document .app
Re: Unjustified CPU load with a simple Document .app
- Subject: Re: Unjustified CPU load with a simple Document .app
- From: Daniel Zitter <email@hidden>
- Date: Fri, 6 Jun 2003 21:26:07 -0700
Subject: Unjustified CPU load with a simple Document .app
From: ettore <email@hidden>
To: email@hidden
Hello, I'm learning cocoa and I made a simple Document app called
SimpleTimer which starts an NSTimer and prints a message on the
console when the timer ends. My project is composed by the MyDocument
class, its Nib and the MainMenu.nib which is untouched. The GUI has
- 2 TextFields: "minutes to wait" and the "message" to be displayed.
- 2 buttons: "Start" and "Invalidate": "Start" creates the NSTimer and
"Invalidate" calls the `invalidate:' method of NSTimer.
MyDocument has a NSTimer, a NSMutableDictionary for saving the data of
the timer document, and the UI outlets. MyDocument is the delegate of
the 2 NSTextFields, and it implements
-(BOOL)control:(NSControl *)control textShouldEndEditing:(NSText
*)fieldEditor.
It also implements the method the NSTimer will fire when it finishes
counting.
Everything works except for one fact: when a document is open, and
SimpleTimer is the active application (i.e. its menus are visible on
the menubar at the top of the screen) I notice - with `top' from the
terminal - a big CPU load (20%-30%) related to SimpleTimer, even when
the NSTimer object is not allocated and the mouse is not moving. If I
close all the documents (not the app), or either click on some other
application, the SimpleTimer load drops to 0. I tried to remove the
NSTimer, the delegate method and the MutableDictionary from the class
with no effect. I know `top' is not reliable, but I can't understand
why my trivial application has a load of 30% while every other
application has a load of 0. BTW, my app is very similar (if not
identical) in the structure to Hillegaas' `RaiseMen.app' listed in
his book `Cocoa Programming with Mac OS X', but of course his
application doesn't have my issue.
Any help would be greatly appreciated. I used OS X 10.2.6, PB 2.0.1
(July 2002
Dev Tools), IB 2.3.1 (v286). Thanks in advance.
Ettore
top is more than adequate for rough estimates, and 30% CPU usage is
colloquially known as the side of a barn.
As you suspect, your application should not be use much CPU at all.
Something else must be going on.
Any chance the code you've posted differs from the code which loaded
the CPU?
(Since you don't explicitly invalidate the timer other than in
invalidateTimer:, you might be collecting a number of firing timers if
the timers are repeating or the enable/disable button code wasn't
working right. The code posted doesn't seem to do that, but I don't
want to assume.)
In general I think it's a good habit to invalidate a timer (even
non-repeating) when you are done with it, which is usually in the
action invoked by the timer. That way, even if there is a
retain/release/autorelease bug in your code, you don't end up with a
drastically worse situation: inconceivable sporadic behavior.
- (void)openPanelWithTimer:(NSTimer *)aTimer
{
NSAssert(aTimer == timer, @"bizarro world");
[timer invalidate]; // harmless if timer is already invalid
[timer release]; // don't need this anymore
timer = nil; // no really, I mean it
// do the thing with the stuff here
}
}
Cheers,
Dan
_______________________________________________
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.