访问电话簿时的ios内存泄漏 [英] ios memory leak when accessing phone book

查看:51
本文介绍了访问电话簿时的ios内存泄漏的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

- (NSArray *)getAllContacts{

    CFErrorRef *error = nil;


    ABAddressBookRef addressBook = ABAddressBookCreateWithOptions(NULL, error);

    __block BOOL accessGranted = NO;
    if (ABAddressBookRequestAccessWithCompletion != NULL) { // we're on iOS 6
        dispatch_semaphore_t sema = dispatch_semaphore_create(0);
        ABAddressBookRequestAccessWithCompletion(addressBook, ^(bool granted, CFErrorRef error) {
            accessGranted = granted;
            dispatch_semaphore_signal(sema);
        });
        dispatch_semaphore_wait(sema, DISPATCH_TIME_FOREVER);

    }
    else { // we're on iOS 5 or older
        accessGranted = YES;
    }

    if (accessGranted == YES) {

#ifdef DEBUG
        NSLog(@"Fetching contact info ----> ");
#endif


        ABAddressBookRef addressBook = ABAddressBookCreateWithOptions(NULL, error);
        ABRecordRef source = ABAddressBookCopyDefaultSource(addressBook);
        CFArrayRef allPeople = ABAddressBookCopyArrayOfAllPeopleInSourceWithSortOrdering(addressBook, source, kABPersonSortByFirstName);
        CFIndex nPeople = ABAddressBookGetPersonCount(addressBook);
        NSMutableArray* items = [NSMutableArray arrayWithCapacity:nPeople];


        for (int i = 0; i < nPeople; i++)
        {
            ContactsData *contacts = [ContactsData new];

            ABRecordRef person = CFArrayGetValueAtIndex(allPeople, i);

            //get First Name and Last Name

            contacts.firstNames = (__bridge NSString*)ABRecordCopyValue(person, kABPersonFirstNameProperty);

            contacts.lastNames =  (__bridge NSString*)ABRecordCopyValue(person, kABPersonLastNameProperty);

            if (!contacts.firstNames) {
                contacts.firstNames = @"";
            }
            if (!contacts.lastNames) {
                contacts.lastNames = @"";
            }

            // get contacts picture, if pic doesn't exists, show standart one

            //            NSData  *imgData = (__bridge NSData *)ABPersonCopyImageData(person);
            //            contacts.image = [UIImage imageWithData:imgData];
            //            if (!contacts.image) {
            //                contacts.image = [UIImage imageNamed:@"NOIMG.png"];
            //            }
            //            //get Phone Numbers
            //
            //            NSMutableArray *phoneNumbers = [[NSMutableArray alloc] init];
            //
            //            ABMultiValueRef multiPhones = ABRecordCopyValue(person, kABPersonPhoneProperty);
            //            for(CFIndex i=0;i<ABMultiValueGetCount(multiPhones);i++) {
            //
            //                CFStringRef phoneNumberRef = ABMultiValueCopyValueAtIndex(multiPhones, i);
            //                NSString *phoneNumber = (__bridge NSString *) phoneNumberRef;
            //                [phoneNumbers addObject:phoneNumber];
            //
            //                //NSLog(@"All numbers %@", phoneNumbers);
            //
            //            }


            //            [contacts setNumbers:phoneNumbers];

            //get Contact email

            NSMutableArray *contactEmails = [NSMutableArray new];
            ABMultiValueRef multiEmails = ABRecordCopyValue(person, kABPersonEmailProperty);

            for (CFIndex i=0; i<ABMultiValueGetCount(multiEmails); i++) {
                CFStringRef contactEmailRef = ABMultiValueCopyValueAtIndex(multiEmails, i);
                NSString *contactEmail = (__bridge NSString *)contactEmailRef;

                [contactEmails addObject:contactEmail];
                // NSLog(@"All emails are:%@", contactEmails);

            }

            [contacts setEmails:contactEmails];


            //---- if no mail id found then dont add contact
            if (contactEmails && contactEmails.count != 0) {
                [items addObject:contacts];
            }


#ifdef DEBUG
            //            NSLog(@"Person is: %@ # %@", contacts.firstNames,contacts.lastNames);
            //            NSLog(@"Phones are: %@", contacts.numbers);
            //            NSLog(@"Email is:%@", contacts.emails);
#endif




        }
        return items;



    } else {
#ifdef DEBUG
        NSLog(@"Cannot fetch Contacts :( ");
#endif
        return nil;


    }

}

崩溃日志: 8月6日19:11:08 iPad:获取联系信息---->

crash log: Aug 6 19:11:08 IPad : Fetching contact info ---->

8月6日19:11:08 IPad :(警告)[ABLog]:实际的默认来源不存在

Aug 6 19:11:08 IPad : (Warn ) [ABLog]: Actual default source doesn't exist

8月6日19:11:57 iPad com.apple.debugserver-310.2 [449]:得到了一个'k'数据包,杀死了劣等进程.

Aug 6 19:11:57 IPad com.apple.debugserver-310.2[449] : Got a 'k' packet, killing the inferior process.

8月6日19:11:57 iPad com.apple.debugserver-310.2 [449]:发送ptrace PT_KILL终止劣等进程.

Aug 6 19:11:57 IPad com.apple.debugserver-310.2[449] : Sending ptrace PT_KILL to terminate inferior process.

8月6日19:11:57 IPad com.apple.debugserver-310.2 [449]:38 +82.923904秒[01c1/060b]:错误::: ptrace(请求= PT_THUPDATE,pid = 0x01c2,tid = 0x2603,信号= 0)错误=资源繁忙(0x00000010)

Aug 6 19:11:57 IPad com.apple.debugserver-310.2[449] : 38 +82.923904 sec [01c1/060b]: error: ::ptrace (request = PT_THUPDATE, pid = 0x01c2, tid = 0x2603, signal = 0) err = Resource busy (0x00000010)

8月6日19:11:57 IPad com.apple.debugserver-310.2 [449]:39 +0.005248秒[01c1/060b]:错误::: task_info(target_task = 0x1c0b,flavor = TASK_BASIC_INFO,task_info_out => 0x27d93d48 ,task_info_outCnt => 8)err =(os/kern)无效参数(0x00000004)

Aug 6 19:11:57 IPad com.apple.debugserver-310.2[449] : 39 +0.005248 sec [01c1/060b]: error: ::task_info ( target_task = 0x1c0b, flavor = TASK_BASIC_INFO, task_info_out => 0x27d93d48, task_info_outCnt => 8 ) err = (os/kern) invalid argument (0x00000004)

8月6日19:11:57 IPad com.apple.launchd 1 ( UIKitApplication:com.baltech.gaurav [0x7f50] [450]):(UIKitApplication:com.baltech.gaurav [0x7f50])退出:被杀:9

Aug 6 19:11:57 IPad com.apple.launchd1 (UIKitApplication:com.baltech.gaurav[0x7f50][450]) : (UIKitApplication:com.baltech.gaurav[0x7f50]) Exited: Killed: 9

8月6日19:11:57 iPad com.apple.debugserver-310.2 [449]:等待20毫秒以获取进程的状态(状态=已退出)

Aug 6 19:11:57 IPad com.apple.debugserver-310.2[449] : Waited 20 ms for process to be reaped (state = Exited)

8月6日19:11:57 IPad com.apple.debugserver-310.2 [449]:40 +0.025414秒[01c1/1207]:错误::: read(-1,0x4089ec,18446744069414585344)=> -1错误=错误的文件描述符(0x00000009)

Aug 6 19:11:57 IPad com.apple.debugserver-310.2[449] : 40 +0.025414 sec [01c1/1207]: error: ::read ( -1, 0x4089ec, 18446744069414585344 ) => -1 err = Bad file descriptor (0x00000009)

8月6日19:11:57 iPad com.apple.debugserver-310.2 [449]:正在退出.

Aug 6 19:11:57 IPad com.apple.debugserver-310.2[449] : Exiting.

8月6日19:11:57 IPad背板[28]:应用程序'UIKitApplication:com.baltech.gaurav [0x7f50]'异常退出,信号为9:杀死:9

Aug 6 19:11:57 IPad backboardd[28] : Application 'UIKitApplication:com.baltech.gaurav[0x7f50]' exited abnormally with signal 9: Killed: 9

- 预先感谢

推荐答案

__ bridge_transfer

contacts.firstNames = (__bridge_transfer NSString*)ABRecordCopyValue(person, kABPersonFirstNameProperty);

请参阅 Apple文档 :

__ bridge CFRelease 在Objective-C和Core Foundation之间转移指针,而没有所有权转移.

__bridge or CFRelease transfers a pointer between Objective-C and Core Foundation with no transfer of ownership.

__ bridge_transfer CFBridgingRelease 将非Objective-C指针移动到Objective-C,并将所有权转移到ARC. ARC负责放弃对象的所有权.

__bridge_transfer or CFBridgingRelease moves a non-Objective-C pointer to Objective-C and also transfers ownership to ARC. ARC is responsible for relinquishing ownership of the object.

这篇关于访问电话簿时的ios内存泄漏的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆