Is __bridge_transfer valid for a NULL object

Let's say a method returns CFErrorRefvia a pointer. This return error may be NULL. So it would be safe to execute __bridge_transferstill or should I check on NULL.

eg.

CFErrorRef cfError;
ABAddressBookRef addressBookRef = ABAddressBookCreateWithOptions(NULL, &cfError);

NSError *error = (__bridge_transfer NSError *)cfError;

I do not see any mention of this in the documentation, but in the documentation it CFReleasesays This value must not be NULL. https://developer.apple.com/library/mac/documentation/CoreFoundation/Reference/CFTypeRef/Reference/reference.html#//apple_ref/c/func/CFRelease

+4
source share
4 answers

A direct answer to the question: yes, you can use __bridge_transferon NULL. But this is not the right question.

ABAddressBookCreateWithOptions. , error:

, . . " ".

.

  • error .
  • error nil/NULL/0 () .

. API- . , CFError -1. "", NULL , , -1 NSError, , .

, CFError, ABAddressBookCreateWithOptions, NULL.

CFErrorRef cfError;
NSError *error;
ABAddressBookRef addressBookRef = ABAddressBookCreateWithOptions(NULL, &cfError); 
if (addressBookRef == NULL) {
    error = (__bridge_transfer NSError *)cfError;
}

, , , , - 0-. , (, _thing1 _thing2 ):

- (id)bar {
    if (_thing1) return NO;
    if (_thing2) return 0;
    return NULL;
}

, , , ... , . , - :

- (NSNumber *)someCalculationWithError:(NSError *)error {
   return 0; // meant to return @(0)
}
+1

NULL.

ARC - . __bridge_transfer, . , cfError NULL , .

ARC error, error , no-op.

+3

NULL, NULL. CF , if. if (addressBookRef == NULL) { /* your error handling here */}

, NULL. , , , NULL nil. -. . nil Objective-C , . NULL CFRelease() CGRetain()

+2

Unlike NSObjects, sending messages to NULL CF objects is not okay. I don’t know what is specifically related to bridges, but I would suggest that no, casting a CF to NSObject using __bridge_transfer is NOT normal.

Why not try and see? Pass it to a variable in the local scope of the instance method. Thus, as soon as the method goes beyond the scope, the system should try to free the object.

0
source

Source: https://habr.com/ru/post/1548148/


All Articles