In the developer documentation, it says:
If your application or thread is long-lived and potentially generates a lot of autoreleased objects, you should periodically drain and create autorelease pools (like the Application Kit does on the main thread); otherwise, autoreleased objects accumulate and your memory footprint grows. If, however, your detached thread does not make Cocoa calls, you do not need to create an autorelease pool.
I was wondering what the best way to do this is. I have several methods I think would work, but don't know which is the "best". I currently have a method that start the thread and keeps it waiting for operations to perform:
- (void)startThread
{
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
accessoryRunLoop = [NSRunLoop currentRunLoop];
//Add a dummy port to avoid exiting the thread due to no ports being found
[accessoryRunLoop addPort:[NSMachPort port] forMode:NSDefaultRunLoopMode];
while(accessoryThreadIsRunning)
{
//Keep the thread running until accessoryTheadIsRunning == NO
[accessoryRunLoop runMode:NSDefaultRunLoopMode beforeDate:[NSDate distantFuture]];
}
[pool release];
}
My options I can think of are:
1) Add a counter in the while(accessoryThreadIsRunning) so that every 50 or 100 times it will drain the autorelease pool and create a new one.
2) Every time I perform a method in that thread (using performSelector: onThread:), I can create an autorelease pool and then release it at the end of the method.
3) Make a timer so that a pool is drained and then created periodically.
I think that option 1 is the best, but would like to know if there is a different way I should be doing this. Thanks!