5

我难住了。

我正在尝试获取一个人拥有的所有电子邮件地址的列表。我正在使用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。ABRecordCopyValue我每次都从 NSLog 中得到泄漏,但代码似乎运行良好。

另外,我可以注释掉

CFRelease(multi);

最后,它做同样的事情。静态编译错误,但运行代码。

所以如果没有泄漏,这个函数就会崩溃。为了防止崩溃,我需要大量记忆。两者都不是一个很好的解决方案。

谁能指出发生了什么?

4

2 回答 2

13

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

- (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;
}
于 2011-06-27T04:25:07.527 回答
0

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

ABRecordRef personDealingWith;

看来你不能这样做:

ABRecordRef personDealingWith = person;

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

ABRecordID personID = ABRecordGetRecordID(person);
ABAddressBookRef addressBook = ABAddressBookCreateWithOptions(NULL, NULL);
personDealingWith = ABAddressBookGetPersonWithRecordID(addressBook, personID);
于 2014-06-16T02:50:27.480 回答