__autoreleasing ownership qualifier
Assigning an object to any variables qualified with __autoreleasing is equivalent to calling the autorelease method in a non-ARC environment. Let’s see how it works with source code.
@autoreleasepool {
id __autoreleasing obj = [[NSObject alloc] init];
}
It adds an object of NSObject class to the autorelease pool. Let’s see how the compiler translates it.
/* pseudo code by the compiler */
id pool = objc_autoreleasePoolPush();
id obj = objc_msgSend(NSObject, @selector(alloc));
objc_msgSend(obj, @selector(init));
objc_autorelease(obj);
objc_autoreleasePoolPop(pool);
It just works as I explained with Apple’s implementation. (See Chapter 1, Section “Apple’s Implementation of autorelease.”) The autorelease mechanism itself works exactly same as in a non-ARC environment although the source codes are different.
What happens if the object is not obtained by the alloc/new/copy/mutableCopy method group? Let’s see the next example with NSMutableArray class method “array”.
@autoreleasepool {
id __autoreleasing obj = [NSMutableArray array];
}
Let’s check what is different from the previous example.
/* pseudo code by the compiler */
id pool = objc_autoreleasePoolPush();
id obj = objc_msgSend(NSMutableArray, @selector(array));
objc_retainAutoreleasedReturnValue(obj);
objc_autorelease(obj);
objc_autoreleasePoolPop(pool);
Although, objc_retainAutoreleasedReturnValue is used, other autorelease pool-related codes are exactly the same as the previous example.