iOS ARC _objc_rootRetainCount

246 views Asked by At

Xcode8, Mac OS 10.12. ARC

I created a MacOS Command Line Tool Project.

And here is the main function

int main(int argc, const char * argv[]) {
    @autoreleasepool {
            id array = [[NSMutableArray alloc] init];
            NSLog(@"array count is %lu", _objc_rootRetainCount(array));
            _objc_autoreleasePoolPrint();
    }
    return 0;
}

And the result is as expected:

the retainCount of array is 1, and array doesn't register to autorelease pool.

But When I use id array = [NSMutableArray array] . The result changed, and it was not as expected.

The retainCount of array is 2, and array registered to autorelease pool.

It looks like these two functions (objc_retainAutoreleaseReturnValue and objc_retainAutoreleasedReturnValue) didn't work.


Now I create an iOS APP. And here is the code in viewDidLoad

- (void)viewDidLoad {
    // id __weak tmp = nil;
    id array = [NSMutableArray array];
    NSLog(@"%lu", _objc_rootRetainCount(array));
}

The retainCount of array is 1.

But if I uncommented the line id __weak tmp = nil;, the result changed to 2. Very strange..

0

There are 0 answers