3

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

1
  • the property should retain, then you should release... that way the object is the owner rather than the local scope. Commented Mar 14, 2014 at 18:17

2 Answers 2

3

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;
}
Sign up to request clarification or add additional context in comments.

Comments

0

You need to call it like this:

 [self setAddressBook:CFBridgingRelease(myAddressBook)];

12 Comments

It shouldn't be. :) It should be strong as usual (it should be an id), and we just hand it over to ARC to manage until the end of time.
@matt Yes, I know what it should be, but you should have pointed it in your answer, instead of pointing to your book.
@LeoNatan It's not advertising! It's free tutorial material like any other. I've deliberately put this stuff on the Web to make it public and available for free. That's not bad, it's good.
@LeoNatan and that is what I thought I did. Hey, the marketplace decided - you got several upvotes and the checkmark, why be a bad winner? That's why Stack Overflow is great!
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.