gpt4 book ai didi

ios - 地址簿 iOS 中的内存泄漏问题

转载 作者:行者123 更新时间:2023-11-29 12:55:45 25 4
gpt4 key购买 nike

我在从地址簿中获取联系人详细信息时遇到内存泄漏问题。下面是我获取详细信息的代码。谁能帮帮我?

NSMutableDictionary *mycontacts = [[NSMutableDictionary alloc] init];
ABAddressBookRef addressBookRef =ABAddressBookCreateWithOptions(NULL, NULL);

if (ABAddressBookGetAuthorizationStatus() ==kABAuthorizationStatusNotDetermined)
{
ABAddressBookRequestAccessWithCompletion(addressBookRef, ^(bool granted, CFErrorRef error)
{
if (granted)
{
ABAddressBookRef addressBook =ABAddressBookCreateWithOptions(NULL, NULL);
//###MEMORY LEAK LINE
NSArray * allPeople = (__bridge NSArray*) ABAddressBookCopyArrayOfAllPeople(addressBook);
CFIndex numberOfPeople =ABAddressBookGetPersonCount(addressBook);
for( NSUInteger i = 0; i < numberOfPeople; i++)
{
ABRecordRef person =CFArrayGetValueAtIndex((__bridge CFArrayRef)(allPeople), i );
NSString *mobileNumber =[[NSString alloc]init];
NSString *firstName = (__bridge NSString *)(ABRecordCopyValue(person,kABPersonFirstNameProperty));
ABMultiValueRef phoneNumbers = ABRecordCopyValue(person,kABPersonPhoneProperty);
for (CFIndex i = 0; i <ABMultiValueGetCount(phoneNumbers); i++)
{
NSString *phoneNumber = (__bridge_transfer NSString *)ABMultiValueCopyValueAtIndex(phoneNumbers, i);
if(phoneNumber.length>0)
{
mobileNumber=phoneNumber;
mobileNumber= [mobileNumber stringByReplacingOccurrencesOfString:@" " withString:@""];
mobileNumber= [mobileNumber stringByReplacingOccurrencesOfString:@"." withString:@""];
mobileNumber= [mobileNumber stringByReplacingOccurrencesOfString:@"-" withString:@""];
mobileNumber= [mobileNumber stringByReplacingOccurrencesOfString:@"+" withString:@""];
NSString *unfilteredString = mobileNumber;
NSCharacterSet *notAllowedChars = [[NSCharacterSet characterSetWithCharactersInString:@"1234567890"] invertedSet];
NSString*updatedmobileNumber = [[unfilteredString componentsSeparatedByCharactersInSet:notAllowedChars]componentsJoinedByString:@""];

[mycontacts setObject:updatedmobileNumber forKey:firstName];
break;
}
if(phoneNumber !=nil)
{
CFRelease((__bridge CFTypeRef)(phoneNumber));
}

}
if(phoneNumbers !=nil)
{
CFRelease(phoneNumbers);
}

if(person !=nil)
{
CFRelease(person);
}

if(firstName !=nil)
{
CFRelease((__bridge CFTypeRef)(firstName));
}
}
if(allPeople !=nil)
{
CFRelease((__bridge CFTypeRef)(allPeople));
}
}
else
{
// User denied access
// Display an alert telling user the contact could not be added
}
});
}

最佳答案

我可以看到这条线。

ABAddressBookRef addressBook =ABAddressBookCreateWithOptions(NULL, NULL);

所以你应该在使用后释放,如下所示,

CFRelease(addressBook);

关于ios - 地址簿 iOS 中的内存泄漏问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21201009/

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