我有以下代码:
int main(int argc, char *argv[])
{
void *pointer = NULL;
@autoreleasepool {
NSMutableArray *arr = [[NSMutableArray alloc] init];
pointer = (__bridge_retained void *)arr;
NSLog(@"retain count: %d\n", (NSInteger)CFGetRetainCount((__bridge CFMutableArrayRef)arr)); // 2
}
// Why this line retains an object?
NSMutableArray __weak *cocoaArr = (__bridge NSMutableArray *)pointer;
NSLog(@"retain count: %d", (NSInteger) CFGetRetainCount((__bridge CFMutableArrayRef)cocoaArr));
NSLog(@"retain count: %d", (NSInteger)CFGetRetainCount((__bridge CFMutableArrayRef)((__bridge NSMutableArray *)pointer))); // 2
return 0;
}
输出:
2013-05-11 11:44:12.488 __iOS_SimpleConsoleApplication[16667:c07] retain count: 2
2013-05-11 11:44:12.491 __iOS_SimpleConsoleApplication[16667:c07] retain count: 2
2013-05-11 11:44:12.492 __iOS_SimpleConsoleApplication[16667:c07] retain count: 2
有趣的事情......如果我使用这段代码:
NSMutableArray __weak *cocoaArr = (__bridge NSMutableArray *)pointer; // Why this line retains an object?
// NSLog(@"retain count: %d", (NSInteger) CFGetRetainCount((__bridge CFMutableArrayRef)cocoaArr));
NSLog(@"retain count: %d", (NSInteger)CFGetRetainCount((__bridge CFMutableArrayRef)((__bridge NSMutableArray *)pointer))); // 2
输出是:
2013-05-11 11:53:52.416 __iOS_SimpleConsoleApplication[16878:c07] retain count: 2
2013-05-11 11:53:52.421 __iOS_SimpleConsoleApplication[16878:c07] retain count: 1
如果我使用它:
NSMutableArray __weak *cocoaArr = (__bridge NSMutableArray *)pointer; // Why this line retains an object?
NSLog(@"retain count: %d", (NSInteger) CFGetRetainCount((__bridge CFMutableArrayRef)cocoaArr));
// NSLog(@"retain count: %d", (NSInteger)CFGetRetainCount((__bridge CFMutableArrayRef)((__bridge NSMutableArray *)pointer))); // 2
输出是:
2013-05-11 11:54:23.742 __iOS_SimpleConsoleApplication[16904:c07] retain count: 2
2013-05-11 11:54:23.745 __iOS_SimpleConsoleApplication[16904:c07] retain count: 2