问题: 当执行下面InsertNewMobileContactsBriefToClient方法的时候。会有80%+的几率出现系统闪退!
- 程序块
-(void)InsertNewMobileContactsBriefToClient
{
CFErrorRef myError = NULL;
ABAddressBookRef addressBook = nil;
addressBook = ABAddressBookCreateWithOptions(NULL, &myError);
if(myError)
{
if(addressBook)
CFRelease(addressBook);
return;
}
__weak typeof(self) weakSelf = self;
ABAddressBookRequestAccessWithCompletion(addressBook,
^(bool granted, CFErrorRef error) {
if (granted) {
[weakSelf InsertNewContactsToDb:addressBook];
} else {
// Handle the error
}
if(addressBook)
CFRelease(addressBook);
});
}
-(void)InsertNewContactsToDb:(ABAddressBookRef)addressBook
{
CFArrayRef personArray = ABAddressBookCopyArrayOfAllPeople(addressBook);
NSInteger count = CFArrayGetCount(personArray);
NSLog(@"SysContact.InsertNewMobileContactsBriefToClient:手机中共有%ld位联系人",(long)count);
for(int i=0;i<count;i++)
{
[NSThread sleepForTimeInterval:0.01];
ABRecordRef person = CFArrayGetValueAtIndex(personArray, i);
NSInteger personId = ABRecordGetRecordID(person);
//读入到的记录保存到数据库
}
NSLog(@"从手机中新导入了%d个联系人..完成!",totalCount);
if(personArray)
CFRelease(personArray);
}
通过友盟收集到的日志信息为:[Application received signal SIGSEGV]类型的错误,通过反解析发现
以下代码块会闪退
if(addressBook)
CFRelease(addressBook);
解决方法
- 移除方法InsertNewContactsToDb中的以下代码:
if(personArray)
CFRelease(personArray);
原来
CFArrayRef personArray = ABAddressBookCopyArrayOfAllPeople ABAddressBookRef addressBook = ABAddressBookCreateWithOptions(NULL, &myError) 为同一个addressbook对象。
只能释放一个。