0

I am trying to access all the first name value field from the address book. I am using this code for this

CFStringRef firstName = ABRecordCopyValue(aSource, kABPersonFirstNameProperty);
first_name=[NSString stringWithFormat:@"%@",firstName];
CFRealease(firstname)

I am not using ARC. So i need to CFRealease(firstname) at the end. But in my code when i add CFRealease(firstname) my app crashes at this point and without this the app works fine.

But when i try to analyse my app by using analyzer it says object Leaked: object allocated and stored into 'firstname' is not refrenced later in the execution path and has a retain count of +1.

Case is same for midname and last name whose code are given below.

 CFStringRef midname = ABRecordCopyValue(aSource, kABPersonMiddleNameProperty);
 mid_name=[NSString stringWithFormat:@"%@",midname];
 CFRelease(midname);

 CFStringRef lastName = ABRecordCopyValue(aSource, kABPersonLastNameProperty);
 last_name=[NSString stringWithFormat:@"%@",lastName];
 CFRelease(lastName);

Please tell where i am doing it wrong. Thanks in advance.

4

1 に答える 1

0

firstname名への参照を(not )に保存すると仮定すると、参照されているfirstNameオブジェクトを解放しようとするとアプリがクラッシュします。これは、アプリがアドレス帳 (連絡先データベース) にアクセスできない場合に発生する可能性があります。これは、ユーザーがまだアクセスを許可していないためです。これは iOS 6 で行う必要があります。この場合、連絡先は返されません。 次のコードを使用して、アクセスを要求できます。 firstnamenil

-(bool)userGrantedReadAccessToContacts
{
    __block BOOL accessGranted = NO;
    if (ABAddressBookRequestAccessWithCompletion != NULL) { // iOS 6
        dispatch_semaphore_t sema = dispatch_semaphore_create(0);
        ABAddressBookRequestAccessWithCompletion(contactsRef, ^(bool granted, CFErrorRef error) {
            accessGranted = granted;
            dispatch_semaphore_signal(sema);
        });
        dispatch_semaphore_wait(sema, DISPATCH_TIME_FOREVER);
    }
    else { // iOS 5 or earlier
        accessGranted = YES;
    }

    return accessGranted;
}
于 2013-05-27T11:46:43.147 に答える