#import <Foundation/Foundation.h> @interface SampleClass : NSObject - (void)dealloc; - (void)hello; @end @implementation SampleClass - (void)dealloc { NSLog(@"dealloc"); } - (void)hello { NSLog(@"Hello, World!"); } @end int main(int argc, const char * argv[]) { @autoreleasepool { SampleClass *obj = [[SampleClass alloc] init]; [obj hello]; } return 0; }
を実行してみる。@autoreleasepool
をコメントアウトしてもdealloc
は呼び出されてはいるな・・・。
Hopper Disassemblerで見てみると
[@autoreleasepoolあり]
int _main() { r14 = objc_autoreleasePoolPush(); rbx = [[SampleClass alloc] init]; [rbx hello]; [rbx release]; objc_autoreleasePoolPop(r14); return 0x0; }
[@autoreleasepoolなし]
int _main() { rbx = [[SampleClass alloc] init]; [rbx hello]; [rbx release]; return 0x0; }
となっている。これだけ小さいサンプルだとご利益が分からないな・・・。巨大ループの前後を囲むとかだと意味がありそうだが・・・