Question

Running the static analyzer on this piece of code:

- (id) readForeignPref
{
 CFPropertyListRef matchStyle = CFPreferencesCopyAppValue(CFSTR("PBXFindMatchStyle"), CFSTR("com.apple.Xcode"));
 return [(id)matchStyle autorelease];
}

yields the following warning:

Call to function 'CFPreferencesCopyAppValue' returns a Core Foundation object with a +1 retain count (owning reference). Core Foundation objects are not automatically garbage collected.

Is this a warning I should fix with somethin ugly like that:

- (id) readForeignPref
{
 CFPropertyListRef matchStyle = CFPreferencesCopyAppValue(CFSTR("PBXFindMatchStyle"), CFSTR("com.apple.Xcode"));
 id nsMatchStyle = [(id)matchStyle copy];
 if (matchStyle) {
  CFRelease(matchStyle);
 }
 return [nsMatchStyle autorelease];
}

or is this just a false positive, given that the copied object is toll-free bridged?

Was it helpful?

Solution

Try this:

- (id) readForeignPref
{
      CFPropertyListRef matchStyle = CFPreferencesCopyAppValue(CFSTR("PBXFindMatchStyle"), CFSTR("com.apple.Xcode"));
      return [(id)CFMakeCollectable(matchStyle) autorelease];
}

Without the CFMakeCollectable, this will leak in GC, because a CFRetain is different than an ObjC -retain. A CFRetain disables garbage collection of that object, and needs CFMakeCollectable to enable it.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top