4
votes

According to this Apple page, I've read that when interacting with Cocoa on a POSIX thread that I should create a NSAutoreleasePool.

If you are making Cocoa calls outside of the Application Kit’s main thread—for example if you create a Foundation-only application or if you detach a thread—you need to create your own autorelease pool.

Unfortunately, using NSAutoreleasePool is disallowed in ARC.

What should I do then to guarantee that there is always an pool available for any ARC code that is autoreleased?

Thanks!

1

1 Answers

5
votes

Use @autoreleasepool.

@autoreleasepool
{
    // make Cocoa calls here
}

This allows the compiler to reason properly about the lifetime of objects that cross the pool's boundary, which is a requirement for ARC. (That's why you can't use NSAutoreleasePool.) As a bonus, it's also faster.