ABAddressBookSave导致启用EXC_BAD_ACCESS ARC

时间:2013-12-06 19:23:24

标签: ios objective-c abaddressbook

我写的代码一直在工作,直到我试图将保存移到新线程。 在ViewController.h中我分配了一个ABAddressBookRef:

ViewController.h

ABAddressBookRef addressBook;

并在ViewController.m文件中创建了一个地址簿对象

ViewController.m
addressBook = ABAddressBookCreateWithOptions(NULL, anError);
/// ... getting a person name and modify it. the code is OK. tested before the NSThread thing
NSThread *saveThread = [[NSThread alloc] initWithTarget:self selector:@selector(saveAddressBook:) object:(__bridge id)addressBook];                     
[saveThread start];

,saveThread方法是:

- (void)saveAddressBook:(id)ab
{
    bool didSave;   
    CFErrorRef error = NULL;

    didSave = ABAddressBookSave((__bridge ABAddressBookRef)ab, &error);
    if (didSave) { NSLog(@"Saved.");}

}

ABAddressBookSave行崩溃了应用程序:

  

主题6:EXC_BAD_ACCESS(代码= 1,地址= 0x65504289)

我知道错误是由于内存故障,但由于我使用ARC,因此无法发布任何内容。

1 个答案:

答案 0 :(得分:1)

这似乎是因为您无法跨多个线程共享ABAddressBookRef的实例(请参阅此Why would ABAddressbookRef need to be created for each thread?)。