IOS SEGV_ACCERR 异常

bugly定位IOS SEGV_ACCERR 异常, 各位大神帮忙看下代码哪里有问题

- (void)peoplePickerNavigationController:(ABPeoplePickerNavigationController*)peoplePicker
                         didSelectPerson:(ABRecordRef)person
                                property:(ABPropertyID)property
                              identifier:(ABMultiValueIdentifier)identifier {
    
    [YZTBaseHudTips hideHud:self.view];
    [[UIApplication sharedApplication] setStatusBarStyle:UIStatusBarStyleLightContent];
    
    ABMutableMultiValueRef name = ABRecordCopyCompositeName(person);
    
    if (property == kABPersonPhoneProperty) {
        
        ABMutableMultiValueRef phone = ABRecordCopyValue(person, kABPersonPhoneProperty);
        
        //根据电机的哪一行对应的identifier取出所在的索引
        
        int index = ABMultiValueGetIdentifierAtIndex(phone, identifier);
        
        CFStringRef phoneRef = ABMultiValueCopyValueAtIndex(phone, index);
        [self updateMobileCell:(__bridge NSString *)name phone:(__bridge NSString *)(phoneRef) peoplePicker:peoplePicker];
        
        CFRelease(phoneRef);
        CFRelease(phone);
    }
    CFRelease(name);
}

crash 上报这一行出问题: int index = ABMultiValueGetIdentifierAtIndex(phone, identifier);

下边是错误堆栈

libobjc.A.dylib    objc_retain + 16
1 cardloan    -[ApplyContactsInfoViewController updateMobileCell:phone:peoplePicker:] (ApplyContactsInfoViewController.m:448)
2 cardloan    -[ApplyContactsInfoViewController peoplePickerNavigationController:didSelectPerson:property:identifier:] (ApplyContactsInfoViewController.m:540)
3 ContactsUI    -[CNContactPickerViewController pickerDidSelectContact:property:] + 296
4 ContactsUI    -[CNContactPickerHostViewController pickerDidSelectContact:property:] + 100
5 ContactsUI    ___71-[CNContactPickerExtensionHostContext pickerDidSelectContact:property:]_block_invoke + 60
6 libdispatch.dylib    __dispatch_call_block_and_release + 24
7 libdispatch.dylib    __dispatch_client_callout + 16
8 libdispatch.dylib    _dispatch_main_queue_callback_4CF + 1844
9 CoreFoundation    ___CFRUNLOOP_IS_SERVICING_THE_MAIN_DISPATCH_QUEUE__ + 12
10 CoreFoundation    ___CFRunLoopRun + 1628
11 CoreFoundation    CFRunLoopRunSpecific + 384
12 GraphicsServices    GSEventRunModal + 180
13 UIKit    UIApplicationMain + 204
14 cardloan    main (main.m:14)
15 libdyld.dylib    _start + 4
阅读 14.2k
2 个回答

使用 ABMultiValueGetIdentifierAtIndex 之前要先检查 index 是否合格,使用 ABMultiValueGetCount 获取数目,然后用 identifier 比较用户点击了那一行。例如:

ABMultiValueRef multiPhones = ABRecordCopyValue(person, kABPersonPhoneProperty);
for(CFIndex i = 0; i < ABMultiValueGetCount(multiPhones); i++) {
    if(identifier == ABMultiValueGetIdentifierAtIndex (multiPhones, i)) {
        CFStringRef phoneNumberRef = ABMultiValueCopyValueAtIndex(multiPhones, i);
        CFRelease(multiPhones);
    }
}

参考:http://stackoverflow.com/a/12...

SEGV_ACCERR一般是内存问题,在64位上比较常见,比如多线程操作某变量,堆栈非法访问等。

新手上路,请多包涵

感觉是identifier这个的问题,ABMultiValueGetIdentifierAtIndex 可能会对identifier的引用计数加减。如果ABMultiValueGetIdentifierAtIndex是异步或者其它线程的话,objc_retain
(identifier)就可能出错。

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题