0

私は次のコードを持っています:

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
4

0 に答える 0