I have following code:
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;
}
Output:
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
Interesting thing... if I use this code snippet:
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
Output is:
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
If I use this:
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
Output is:
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