gpt4 book ai didi

ios - 释放 ABAddressBookRef 时崩溃

转载 作者:行者123 更新时间:2023-12-02 05:00:51 24 4
gpt4 key购买 nike

我有这个枚举地址簿联系人的代码 -

  +(void)enumerateAddressBookContacts:(void(^)(NSString *name, NSArray *phoneNumbers, NSArray *emailAddresses, NSData *imageData, NSString *recordId))enumerationBlock  failure:(void (^)( NSError *error))failure{

[self requestAddressBookPermissionsWithCompletion:^{
if (enumerationBlock)
{
CFErrorRef error = nil;
ABAddressBookRef addressBook = ABAddressBookCreateWithOptions(NULL, &error);
CFArrayRef addressBookPeople = ABAddressBookCopyArrayOfAllPeople (addressBook);
NSArray* people = (__bridge NSArray*)addressBookPeople;

if ([people count])
{

for (NSUInteger i = 0; i < [people count]; i++)
{
ABRecordRef person = (__bridge ABRecordRef)people[i];

NSArray *phoneNumbers = [self phoneNumbersOfPerson:person];
NSArray *emailAddresses = [self emailAddressesOfPerson:person];
NSString *fullName = [self fullNameOfPerson:person];
NSData *imageData = [self imageDataOfPerson:person];
NSString *recordId = [NSString stringWithFormat:@"%d", ABRecordGetRecordID(person)];

void (^block)(NSString*, NSArray *, NSArray *, NSData*, NSString*) = [enumerationBlock copy];
block(fullName,phoneNumbers,emailAddresses,imageData,recordId);

}
}
else
{
//No contacts in addressbook
if (failure) {
NSError *error = [NSError errorWithDomain:kAddressBook code:kAddressBookUploadFailReasonEmptyAddressBook userInfo:@{NSLocalizedDescriptionKey:kAddressBookUploadFailReason[kAddressBookUploadFailReasonEmptyAddressBook]}];
failure(error);
}
}

if (addressBookPeople)
{
CFRelease(addressBookPeople);
}

if (addressBook)
{
CFRelease (addressBook);
}
}

} failure:^(NSError *error) {
if (failure) {
failure(error);
}
}];
}

在方法结束时释放 ABAddressBookRef 对象时发生崩溃。

这是其中一种枚举方法的示例(它们的作用相同)-

+ (NSArray*)phoneNumbersOfPerson:(ABRecordRef)person
{
ABMutableMultiValueRef phones = ABRecordCopyValue (person, kABPersonPhoneProperty);
NSMutableArray *phoneNumbers = [NSMutableArray array];
CFIndex phonesNumberCount = ABMultiValueGetCount (phones);
if (phonesNumberCount > 0)
{
for (CFIndex index = 0; index < phonesNumberCount; index++)
{
CFStringRef phoneValue = ABMultiValueCopyValueAtIndex (phones, index);
[phoneNumbers addObject:(__bridge_transfer NSString *)phoneValue];

if (phoneValue)
{
CFRelease (phoneValue);
}
}
}

if (phones)
{
CFRelease (phones);
}

return phoneNumbers;
}

当我删除版本时,我不会崩溃,但我认为会有泄漏。知道原因是什么吗?

谢谢

最佳答案

很好地找到了问题,我相信它可以帮助其他用户 -

看起来(__bridge_transfer NSString *)已经减少了CFStringRef的保留计数,所以没有必要释放它。

看起来 __bridge 的情况并非如此

关于ios - 释放 ABAddressBookRef 时崩溃,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22742784/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com