objective-cexc-bad-accessvideo-toolboxcfdictionary

Converting NSDictionary to CFDictionary in VideoToolbox


What's the difference between

const void *keys[] = { kCVPixelBufferPixelFormatTypeKey };
OSStatus pixelFormatType = kCVPixelFormatType_32BGRA;
CFNumberRef pixelFormatTypeRef = CFNumberCreate(kCFAllocatorDefault, kCFNumberSInt32Type, &pixelFormatType);
const void *values[] = { pixelFormatTypeRef };
CFDictionaryRef destinationImageBufferAttrs = CFDictionaryCreate(kCFAllocatorDefault, keys, values, 1, NULL, NULL);

and

CFDictionaryRef destinationImageBufferAttrs = (__bridge CFDictionaryRef)(@{(NSString*)kCVPixelBufferPixelFormatTypeKey:@(kCVPixelFormatType_32BGRA)});

?

I am getting an EXC_BAD_ACCESS error if I use the second one. Why?


Solution

  • In your first code sample, the reference stored into destinationImageBufferAttrs is owned and must be later released using CFRelease (or transferred to ARC control).

    In the second code sample, the reference stored into destinationImageBufferAttrs is under ARC control and ARC can free it immediately after the assignment as there are no longer ARC-owned references to it.

    Change __bridge to __bridge_retained to transfer ownership from ARC to your own code, you will then be responsible for calling CFRelease for the object.