• Open Menu Close Menu
  • Apple
  • Shopping Bag
  • Apple
  • Mac
  • iPad
  • iPhone
  • Watch
  • TV
  • Music
  • Support
  • Search apple.com
  • Shopping Bag

Lists

Open Menu Close Menu
  • Terms and Conditions
  • Lists hosted on this site
  • Email the Postmaster
  • Tips for posting to public mailing lists
Re: NSThreads
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: NSThreads


  • Subject: Re: NSThreads
  • From: Trevor Strohman <email@hidden>
  • Date: Wed, 7 May 2003 12:53:43 -0700

Thierry Passeron writes:
-(void)loop
{
[ startDate retain ];
[ endDate retain ];
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];

if( running ){
while( ![[[ NSDate date ] earlierDate: endDate ] isEqual: endDate ] &&
running ){
//[ self increment ]; // the method that increment the progress
bar ...
}
(...) // some stuff
}
[ pool release ];
[ startDate release ];
[ endDate release ];
}

In the while test in every loop, you create a NSDate object ( [NSDate date] ). This object is returned to you autoreleased, and should go away when you break out of the loop and send [pool release]. In the meantime, this object will sit around, eating RAM.

In your second try:

while( running ){
NSDate * currentD = [ NSDate date ]; // retainCount = 1
NSDate * earlierD = [ currentD earlierDate: endDate ]; //
retainCount = 1
if( [ earlierD isEqual: startDate ] ){
running = FALSE;
}else
[ self increment ];
[ currentD release ]; // retainCount = 0
[ earlierD release ]; // retainCount = 0
}

you are releasing currentD and earlierD, which are already autoreleased. I imagine your program crashed when you later released the autorelease pool, since it then tried to release lots of freed objects. The memory growth you were seeing was likely because of internal bookkeeping done by the autorelease pool.

The straightforward solution here is to move the autorelease pool into the loop:

while( running ) {
NSAutoreleasePool* loopPool = [[NSAutoreleasePool alloc] init];
NSDate* currentDate = [NSDate date];
NSDate* earlierDate = [currentDate earlierDate:endDate];

if( [earlierDate isEqual:startDate] ) {
running = FALSE;
} else {
[self increment];
}

[loopPool release];
}

However, I'd like to suggest some possible improvements. First, is it necessary to update the status bar as fast as you can? Would you be satisfied with an update every 1/4 second? That would use significantly less CPU time. You can do that by putting:
[NSThread sleepUntilDate:[NSDate dateWithTimeIntervalSinceNow:0.25]];
right before the [loopPool release] line.

An even simpler solution is to use NSTimer to send the increment message every 1/4 second, which saves you the trouble of doing the loop, thread, and autorelease pool stuff in the first place. However, if increment is going to grow into something that takes some time, you might want to keep the thread around.

Trevor Strohman
Ampersandbox
_______________________________________________
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.

  • Follow-Ups:
    • Re: NSThreads
      • From: Dustin Voss <email@hidden>
  • Prev by Date: RE: Masking a Textfield
  • Next by Date: Re: Tracking mouse activity in NSToolbar?
  • Previous by thread: NSThreads
  • Next by thread: Re: NSThreads
  • Index(es):
    • Date
    • Thread