Why does the static analyzer show that this bridge NSNumber will flow under ARC?

When I run Clath Static Analyzer against the code that I converted to use ARC, it tells NSNumber in this code block as a leak:

NSNumber *temporaryNumber = [NSNumber numberWithFloat:0.85]; CFNumberRef compressionQuality = CFBridgingRetain(temporaryNumber); CFDictionarySetValue(snapshotMetaAndOpts, kCGImageDestinationLossyCompressionQuality, compressionQuality); CFRelease(compressionQuality); 

The analyzer indicates that the NSNumber created and stored in the temporary number ends with a +1 after all of this is completed and therefore leaks. I know that I could just as easily do

 CFDictionarySetValue(snapshotMetaAndOpts, kCGImageDestinationLossyCompressionQuality, (__bridge CFNumberRef)[NSNumber numberWithFloat:0.85]); 

but I'm still trying to understand the exact actions of the bridge in ARC, so I'm trying to figure out the above. The actual analyzer output is as follows:

Visual depiction of static analyzer results

The way I read CFBridgingRetain() and __bridge_retained is that they transfer ownership of the ARC-managed NSObject to the Core Foundation, increasing the number of deductions by 1. I balance this with the corresponding CFRelease() . I expected that NSNumber would be created as an object with auto-implementation and therefore would be completely balanced by the ARC.

Similarly, if I do the following using a simple __bridge cast:

 NSNumber *temporaryNumber = [NSNumber numberWithFloat:0.85]; CFNumberRef compressionQuality = (__bridge CFNumberRef)temporaryNumber; CFDictionarySetValue(snapshotMetaAndOpts, kCGImageDestinationLossyCompressionQuality, compressionQuality); CFRelease(compressionQuality); 

A static analyzer gives this a clean health score.

Am I misinterpreting something in the sense that objects do not have bridges, or is this a mistake in a static analyzer?

+4
source share
1 answer

EDIT: This is a parser error. I have the latest build of the standalone analyzer on my machine and there is no warning. I checked against the build with the current delivery analyzer and got the same results as you. Looks like its already good.

What version of Xcode are you using? I just tested the following.

 int main(int argc, char *argv[]) { @autoreleasepool { CFMutableDictionaryRef dict = CFDictionaryCreateMutable(NULL, 0, &kCFTypeDictionaryKeyCallBacks, &kCFTypeDictionaryValueCallBacks); NSNumber *temporaryNumber = [NSNumber numberWithFloat:0.85]; CFNumberRef compressionQuality = CFBridgingRetain(temporaryNumber); CFDictionarySetValue(dict, CFSTR("Test"), compressionQuality); CFRelease(compressionQuality); CFRelease(dict); } } 

This works as expected and does not give any analyzer warnings. This applies to the latest 4.2 to 10.7 using the iOS SDK.

+5
source

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


All Articles