Re: Thread memory leak
Re: Thread memory leak
- Subject: Re: Thread memory leak
- From: Mason Mark <email@hidden>
- Date: Fri, 06 Jun 2003 13:40:04 -0700
--On Friday, June 6, 2003 12:54 PM -0700 "John C. Randolph" <email@hidden>
wrote:
On Friday, June 6, 2003, at 09:43 AM, Chuck Rice wrote:
I have a thread defined that runs async to the rest or my program to
keep a USB attached LDC display updated with current status
information. Works great, except for this huge memory leak! :( The
memory leak goes away if I comment out this line in the code:
[NSThread sleepUntilDate:[NSDate
dateWithTimeIntervalSinceNow:0.001]];
With this line active I get a 10 to 20 Meg memory leak every minute. I
am still an obj-C newbie, so forgive me if it is obvious. -Chuck-
Well, in the line of code above, you're creating a new NSDate, which will
hang around until the autorelease pool gets released. If you're
generating a thousand of these a second (as the code above would seem to
indicate), the memory used would grow as you describe.
So, let me be the first to ask you the question that all new Cocoa
programmers get asked: "What, exactly, are you trying to do?" ;-)
I suspect that you actually want something to happen every milisecond, in
which case you should look into using the NSTimer class.
-jcr
I thought NSTimer had a resolution of something around 30-50ms?
Also, while it looks like the advice already received on this thread is
correct (not releasing the NSDate instances is causing the "leak"), I have
always guessed in a very tight loop like this, sleeping the thread like
this would be more efficient:
#include <unistd.h>
...
usleep(1000);
I have never profiled that or anything, so I can't definitively say, but it
seems NSDate instantiation would be unnecessary, and might be slow (?) in
such tight circumstances as this...
Best regards,
--
Mason Mark
Five Speed Software, Inc.
_______________________________________________
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.