Conceptual question about NSAutoreleasePools

Posted by ryyst on Stack Overflow See other posts from Stack Overflow or by ryyst
Published on 2010-06-05T15:55:42Z Indexed on 2010/06/05 16:02 UTC
Read the original article Hit count: 253

In my Cocoa program, wouldn't a really simple way of dealing with autoreleased objects be to just create a timer object inside the app delegate that calls the following method e.g. every 10 seconds:

if (pool) {
    // Release & drain the current pool to free the memory.
    [pool release];
}

// Create a new pool.
pool = [[NSAutoreleasePool alloc] init];

The only problems I can imagine are:

1) If the above code runs in a separate thread, an object might get autoreleased between the release call to the old pool and the creation of the new pool - that seems highly unlikely though.

2) It's obviously not that efficient, because the pool might get released if there's nothing in it. Likewise, in the 10 second gap, many many objects might be autoreleased, causing the pool to grow a lot.

Still, the above solution seems pretty suitable to small and simple projects. Why doesn't anybody use it? What's the best practice of using NSAutoreleasePools?

© Stack Overflow or respective owner

Related posts about objective-c

Related posts about cocoa