objective-cmemory-managementautomatic-ref-countingretaincounttoll-free-bridging

ARC: __bridge versus __bridge_retained using contextInfo test case


Consider this ARC code:

- (void)main {
    NSString *s = [[NSString alloc] initWithString:@"s"];
    [NSApp beginSheet:sheet 
           modalForWindow:window 
           modalDelegate:self 
           didEndSelector:@selector(sheetDidEnd:returnCode:context:) 
           contextInfo:(__bridge void *)s
    ];
}

- (void)sheetDidEnd:(NSWindow *)sheet returnCode:(NSInteger)returnCode context:(void *)context {
    NSString *s = (__bridge_transfer NSString *)context;
}

Question: on line 7, should __bridge be used, or __bridge_retained, or does it not matter, or does the choice depend on the string's retain count (that is, whether the string is explicitly alloced vs being autoreleased through a class initializer like +[NSString stringWithString:]?


Solution

  • Generally, it is either

    // Object to void *:
    contextInfo:(__bridge void *)s
    
    // void * to object:
    NSString *s = (__bridge NSString *)context;
    

    or

    // Object to void *, retaining the object:
    contextInfo:(__bridge_retained void *)s
    
    // void * to object, transferring ownership.
    // The object is released when s goes out of scope:
    NSString *s = (__bridge_transfer NSString *)context;
    

    In the first case, there is no transfer of ownership, therefore the main program must hold a strong reference to the object as long as the sheet is active.

    In the second case, the object is retained when creating the sheet, and released in the sheetDidEnd: method. It is not required that the main program holds a strong reference, so this is the safe method.