__bridge_transfer和CFBridgingRelease在ARC代码

时间:2018-04-27 03:04:43

标签: objective-c automatic-ref-counting

CFBridgingRelease应与ARC中的__bridge_transfer相同,但在我的代码中,它们会有不同的结果。

int main(int argc, const char * argv[]) {
    @autoreleasepool {
        CFMutableArrayRef cfObject = CFArrayCreateMutable(kCFAllocatorDefault, 0, NULL);
        printf("retain count = %ld\n", CFGetRetainCount(cfObject));
        //id obj = (__bridge_transfer id)(cfObject);
        id obj = CFBridgingRelease(cfObject);
        printf("retain count after the cast = %ld\n", CFGetRetainCount(cfObject));
        NSLog(@"class=%@", obj);

    }
    return 0;
}

如果使用 __bridge_transfer ,结果如下:

retain count = 1
retain count after the cast = 1
2018-04-27 11:02:28.945866+0800 P01[2344:1638938] class=()

如果使用 CFBridgingRelease ,结果如下:

retain count = 1
retain count after the cast = 2
2018-04-27 11:03:30.795628+0800 P01[2363:1658495] class=()

不同的结果只发生在Mac命令行工具中。 如果我将代码放在iOS App中,结果是一样的。混淆!!!

0 个答案:

没有答案