Question

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.

Était-ce utile?

La solution

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?).

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top