【问题标题】:ABAddressBook memory leakABAddressBook 内存泄漏
【发布时间】:2015-05-12 18:52:33
【问题描述】:

我正在尝试与 ABAddressBook 交互,但在分析 Xcode 中的代码时,我遇到了一些无法解决的内存泄漏警告。我收到警告“对象可能泄漏”和“存储到 'allContacts' 中的对象可能泄漏”

+ (void)addNewContact:(BusinessCard *)contact {
ABAddressBookRef addressBookRef = ABAddressBookCreateWithOptions(NULL, nil);
ABRecordRef newContact = ABPersonCreate();
if (contact.firstName) ABRecordSetValue(newContact, kABPersonFirstNameProperty, (__bridge CFStringRef)contact.firstName, nil);
if (contact.familyName) ABRecordSetValue(newContact, kABPersonLastNameProperty, (__bridge CFStringRef)contact.familyName, nil);
if (contact.company) ABRecordSetValue(newContact, kABPersonOrganizationProperty, (__bridge CFStringRef)contact.company, nil);
if (contact.jobTitle) ABRecordSetValue(newContact, kABPersonJobTitleProperty, (__bridge CFStringRef)contact.jobTitle, nil);
if (contact.emailAddress) {
    ABMutableMultiValueRef multiEmail = ABMultiValueCreateMutable(kABMultiStringPropertyType);
    ABMultiValueAddValueAndLabel(multiEmail, (__bridge CFTypeRef)(contact.emailAddress), kABWorkLabel, NULL);
    ABRecordSetValue(newContact, kABPersonEmailProperty, multiEmail, NULL);
    CFRelease(multiEmail);
}

if (contact.details) ABRecordSetValue(newContact, kABPersonNoteProperty, (__bridge CFStringRef)contact.details, nil);


if (contact.phone) {
    ABMutableMultiValueRef phoneNumbers = ABMultiValueCreateMutable(kABMultiStringPropertyType);
    ABMultiValueAddValueAndLabel(phoneNumbers, (__bridge CFStringRef)contact.phone, kABPersonPhoneMainLabel, NULL);
    ABRecordSetValue(newContact, kABPersonPhoneProperty, phoneNumbers, nil);
    CFRelease(phoneNumbers);
}

ABAddressBookAddRecord(addressBookRef, newContact, nil);
CFRelease(newContact);

NSArray *allContacts = (__bridge NSArray *)ABAddressBookCopyArrayOfAllPeople(addressBookRef);
for (id record in allContacts){
    ABRecordRef thisContact = (__bridge ABRecordRef)record;
    if (CFStringCompare(ABRecordCopyCompositeName(thisContact), ABRecordCopyCompositeName(newContact), 0) == kCFCompareEqualTo){
        [[[UIAlertView alloc] initWithTitle:NSLocalizedString(@"Existing contact", @"Title for alert view when adding an existing contact") message:NSLocalizedString(@"A contact already exists with the same name. Do you want to add the new contact?", @"Message for alert view when adding an existing contact")
                           cancelButtonItem:[RIButtonItem itemWithLabel:NSLocalizedString(@"No", @"No")]
                           otherButtonItems:[RIButtonItem itemWithLabel:NSLocalizedString(@"Yes", @"Yes")   action:^{
            [self saveInAddressBook:addressBookRef];
        }], nil] show]; //Potential leak of an object

        NSLog(@"Contact exists");
        return;
    }

}

[self saveInAddressBook:addressBookRef]; //Potential leak of an object stored into 'allContacts'
CFRelease(addressBookRef);
NSLog(@"Contact created");
}

+ (void)saveInAddressBook:(ABAddressBookRef)addressBookRef {
    ABAddressBookSave(addressBookRef, nil);
    [[[UIAlertView alloc] initWithTitle:NSLocalizedString(@"Contact added", @"Title for alert view when contact is successfully added to address book") message:nil delegate:nil cancelButtonTitle:NSLocalizedString(@"OK", @"OK") otherButtonTitles:nil] show];
}

有人知道我该如何解决这些问题吗?

【问题讨论】:

标签: ios objective-c abaddressbook


【解决方案1】:

你忘了

CFRelease((__bridge CFTypeRef) allContacts);

我觉得你可以这样说:

for (id record in allContacts){
    ...
}

CFRelease((__bridge CFTypeRef) allContacts);
[self saveInAddressBook:addressBookRef]; //Potential leak of an object stored into 'allContacts'
CFRelease(addressBookRef);
NSLog(@"Contact created");

【讨论】:

  • 谢谢!我没有在 CFRelease 中进行桥接,这导致它失败。我都尝试过 CFRelease(allContacts) (不出所料没有用),并且 allContacts = nil。你拯救了我的一天:-)
  • eheh 很高兴为您提供帮助 :)
【解决方案2】:

您可以继续使用 APAddressBook,这是一个非常完善的地址簿框架。

或者,通常帮助我修复与地址簿相关的崩溃的流程是遵循 XCode 建议的优化

在 XCode 中转到产品 -> 分析。

完成后您必须看到几个问题。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-10-08
    • 2013-01-20
    • 2011-10-31
    • 2019-08-10
    • 2013-06-24
    • 2011-03-22
    相关资源
    最近更新 更多