从联系人读取电子邮件地址失败,出现奇怪的内存问题

时间:2010-04-10 10:34:13

标签: iphone memory-leaks addressbook contacts

我很难过。

我正在尝试获取一个人拥有的所有电子邮件地址的列表。 我正在使用ABPeoplePickerNavigationController来选择这个人,这一切似乎都很好。我正在设置我的

ABRecordRef personDealingWith;

person参数到

- (BOOL)peoplePickerNavigationController:(ABPeoplePickerNavigationController *)peoplePicker shouldContinueAfterSelectingPerson:(ABRecordRef)person property:(ABPropertyID)property identifier:(ABMultiValueIdentifier)identifier {
到目前为止,一切似乎都很好。 下面的代码第一次执行,一切都很好。 随后运行时,我可以解决问题。首先,代码:

// following line seems to make the difference (issue 1)
// NSLog(@"%d", ABMultiValueGetCount(ABRecordCopyValue(personDealingWith, kABPersonEmailProperty)));

// construct array of emails
ABMultiValueRef multi = ABRecordCopyValue(personDealingWith, kABPersonEmailProperty);
CFIndex emailCount = ABMultiValueGetCount(multi);

if (emailCount > 0) {
    // collect all emails in array
    for (CFIndex i = 0; i < emailCount; i++) {
        CFStringRef emailRef = ABMultiValueCopyValueAtIndex(multi, i);
        [emailArray addObject:(NSString *)emailRef];
        CFRelease(emailRef);
    }
}

// following line also matters (issue 2)
CFRelease(multi);

如果按编写编译,则没有错误或静态分析问题。这与

崩溃

*** -[Not A Type retain]: message sent to deallocated instance 0x4e9dc60

错误。

但是等等,还有更多!我可以通过两种方式解决它。

首先,我可以取消注释函数顶部的NSLog。我每次都从NSLog的ABRecordCopyValue泄漏,但代码似乎运行良好。

另外,我可以注释掉

CFRelease(multi);

最后,它做了完全相同的事情。静态编译错误,但正在运行代码。

因此没有泄漏,此功能崩溃。为了防止崩溃,我需要出血记忆。两者都不是一个很好的解决方案。

有人能指出发生了什么吗?

2 个答案:

答案 0 :(得分:13)

事实证明我没有正确存储ABRecordRef personDealingWith var。我仍然不确定如何正确地做到这一点,但是我没有在另一个例程中执行该功能(稍后执行),而是在委托方法中执行grunt-work,并在我的闲暇时使用派生结果。新的(工作)例程:

- (BOOL)peoplePickerNavigationController:(ABPeoplePickerNavigationController *)peoplePicker shouldContinueAfterSelectingPerson:(ABRecordRef)person {
    // as soon as they select someone, return
    personDealingWithFullName = (NSString *)ABRecordCopyCompositeName(person);
    personDealingWithFirstName = (NSString *)ABRecordCopyValue(person, kABPersonFirstNameProperty);
    // construct array of emails
    [personDealingWithEmails removeAllObjects];
    ABMutableMultiValueRef multi = ABRecordCopyValue(person, kABPersonEmailProperty);
    if (ABMultiValueGetCount(multi) > 0) {
        // collect all emails in array
        for (CFIndex i = 0; i < ABMultiValueGetCount(multi); i++) {
            CFStringRef emailRef = ABMultiValueCopyValueAtIndex(multi, i);
            [personDealingWithEmails addObject:(NSString *)emailRef];
            CFRelease(emailRef);
        }
    }
    CFRelease(multi);
    return NO;
}

答案 1 :(得分:0)

我遇到了类似的问题。问题可能在于您如何设置

ABRecordRef personDealingWith;

您似乎无法做到:

ABRecordRef personDealingWith = person;

因为personDealingWith仍为null。相反,我所做的是:

ABRecordID personID = ABRecordGetRecordID(person);
ABAddressBookRef addressBook = ABAddressBookCreateWithOptions(NULL, NULL);
personDealingWith = ABAddressBookGetPersonWithRecordID(addressBook, personID);