I have this code that uses "__bridge" to cast the ids of colors:
CGColorRef tabColor = (5 == 5
? [UIColor blueColor].CGColor
: [UIColor greenColor].CGColor);
CGColorRef startColor = [UIColor whiteColor].CGColor;
CGColorRef endColor = tabColor;
NSArray *colors = [NSArray arrayWithObjects:(__bridge id)startColor, (__bridge id)endColor, nil];
CGGradientRef gradient = CGGradientCreateWithColors(colorSpace, (__bridge CFArrayRef)colors, locations);
but would:
NSArray *colors = [NSArray arrayWithObjects:(id)CFBridgingRelease(startColor), (id)CFBridgingRelease(endColor), nil];
CGGradientRef gradient = CGGradientCreateWithColors(colorSpace, (CFArrayRef)CFBridgingRetain(colors), locations);
be a better solution?
With NSURL is the same problem
You don't "own" the Core Foundation objects
startColor
,endColor
because they were not returned by a function that has "Create" or "Copy" in its name (compare "The Create Rule" in the "Memory Management Programming Guide for Core Foundation". And because you don't own the objects, you must not "transfer the ownership" to ARC withCFBridgingRelease()
. Sois correct. And
is also correct because
would pass a (+1) retained array to
CGGradientCreateWithColors()
. This would be a memory leak because that function does not release thecolors
argument.