Question

I think I'm being a bone head here...

The static analyzer complains in the -(void)doSomethingInteresting method:

Potential leak of an object stored into myAddressBook

Well, sure, that makes sense; I'm calling a create function without a matching release. But, I don't think I want to release myAddressBook at the end of -doSomethingInteresting because I want it to stick around for the life of the AwesomeObject instance.

AwesomeObject.h

@interface AwesomeObject : NSObject

@property (assign, nonatomic) ABAddressBookRef addressBook;

@end

AwesomeObject.m

@implementation AwesomeObject

- (void)doSomethingInteresting
{
    CFErrorRef error = NULL;
    ABAddressBookRef myAddressBook = ABAddressBookCreateWithOptions(NULL, &error);

    [self setAddressBook:myAddressBook];
}

@end

EDIT: Screen shot attached

Static Analyzer Arrows

Was it helpful?

Solution

The analyzer doesn't understand you wish to assign a retained address book in your property, and thus not release it at the end of doSomethingInteresting.

Change your property to:

@property (strong, nonatomic) id addressBook;

and set it like so:

[self setAddressBook:CFBridgingRelease(addressBook)];

Now, when you want to use the addressBook property, use the (__bridge ABAddressBookRef) cast.

Also, you need to release a potential CFErrorRef object, which may have been created during ABAddressBookCreateWithOptions:

if(error)
{
    CFRelease(error);
    error = NULL;
}

OTHER TIPS

You need to call it like this:

 [self setAddressBook:CFBridgingRelease(myAddressBook)];
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top