1
-(void) vCreateAddressBookAndPopulateContact
{
    self.addressBook =ABAddressBookCreate();
}
+(void) vCreateAddressBookAndPopulateContact
{
    [[RCABAddressBookHandler singleton] vCreateAddressBookAndPopulateContact];
    //[self 
}

代码是正确的。它创建了一个地址簿。当应用程序消失时,地址簿将在 dealloc 释放。

我确实有内存警告,我想知道我应该怎么做?

这就是属性的声明方式

@property (nonatomic) ABAddressBookRef addressBook;

我当然在 dealloc 释放它

-(void)dealloc
{
    [[NSNotificationCenter defaultCenter]removeObserver:self];
    CFRelease(_addressBook);
}

我们得到的警告是:

/business/Dropbox/badgers/RecentContact/RCABAddressBookHandler.m:52:1: Potential leak of an object
/business/Dropbox/badgers/RecentContact/RCABAddressBookHandler.m:51:23: Call to function 'ABAddressBookCreate' returns a Core Foundation object with a +1 retain count
/business/Dropbox/badgers/RecentContact/RCABAddressBookHandler.m:52:1: Object leaked: allocated object is not referenced later in this execution path and has a retain count of +1
4

1 回答 1

1

如果这是您设置的唯一位置_addressBook,您可以使用直接访问来分配它:

- (void)vCreateAddressBookAndPopulateContact
{
    /* not thread safe */
    if (NULL == _addressBook) {
        _addressBook = ABAddressBookCreate();
    }
}

然后将属性更改为readonly.

于 2013-05-17T09:02:35.577 回答