문제

The code I had been written was working until I tried to move the saving to a new thread. in ViewController.h I assigned an ABAddressBookRef:

ViewController.h

ABAddressBookRef addressBook;

and in ViewController.m file created an addressbook object

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];

and the saveThread method is:

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

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

}

ABAddressBookSave line crashes the app:

Thread 6: EXC_BAD_ACCESS (Code=1,address=0x65504289)

I know the error is because of a memory failure but I can't release anything since I'm using ARC.

도움이 되었습니까?

해결책

It appears to be because you cannot share an instance of ABAddressBookRef across multiple threads (see this Why would ABAddressbookRef need to be created for each thread?).

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top