作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我在从地址簿中获取联系人详细信息时遇到内存泄漏问题。下面是我获取详细信息的代码。谁能帮帮我?
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/
我是一名优秀的程序员,十分优秀!