Memory issue (I believe) with an ABRecordRef in Addressbook framework

187 Views Asked by At

I am currently creating some objects modeled that use data from ABRecordRefs — so I parse out strings to represent name, email. I do need a reference to the actual ABRecord because at some point I’ll pass it off to the ABPErsonViewController.

So if assign the ABRecord as a property of my object, it seems I would have a reference to it. But when I pass it off to the controller, I get a bad access for that record. It appears to exist when I inspect it in the debugger — its not nil at least.

Any ideas? Should I use the record identifier as a property and relook and get the record at the point I hand it off to the ABPersonViewController? Kind of curious why it didnt work the other way.

- (NSArray*)allContacts
{
    ABAddressBookRef addressBook = ABAddressBookCreateWithOptions(NULL, NULL);
    CFArrayRef people = ABAddressBookCopyArrayOfAllPeople(addressBook);
    NSInteger count = ABAddressBookGetPersonCount(addressBook);

    NSMutableArray *contacts = [NSMutableArray array];

    for (int i = 0; i < count; i++) {
        ABRecordRef record = CFArrayGetValueAtIndex(people, i);
        ABRecordType type = ABRecordGetRecordType(record);
        NSLog(@"Type = %i", type);
        ALContact *contact = [[ALContact alloc] initWithRecord:record];

        [contacts addObject:contact];
    }



    CFRelease(addressBook);
    CFRelease(people);

    return contacts;
}

- (instancetype)initWithRecord:(ABRecordRef)record
{
    if (self = [super init]) {
        _firstName = (__bridge_transfer NSString *)ABRecordCopyValue(record, kABPersonFirstNameProperty);
        _lastName = (__bridge_transfer NSString *)ABRecordCopyValue(record, kABPersonLastNameProperty);
        _compositeName = (__bridge_transfer NSString *)ABRecordCopyCompositeName(record);


        _record = record;

        NSData *imageData = (__bridge_transfer NSData *)ABPersonCopyImageDataWithFormat(record, kABPersonImageFormatThumbnail);
        _thumbnailImage = [UIImage imageWithData:imageData];

        _emailAddresses = [self parseEmailValuesForRecord:record];
        _phoneNumbers = [self parsePhoneNumberValuesForRecord:record];
        _socialProfiles = [self parseSocialProfileValuesForRecord:record];
    }
    return self;
}
0

There are 0 best solutions below