ABAddressBookRef在iOS中泄露

时间:2011-11-09 12:35:56

标签: ios memory-leaks

我正在解析iOS中的地址簿,但Leaks仪器报告了大量内存泄漏,我似乎无法追踪问题

首先我创建地址簿。

ABAddressBookRef addressBook = ABAddressBookCreate();
CFArrayRef allPeople = ABAddressBookCopyArrayOfAllPeople(addressBook);

这是我怀疑是泄漏的,但是ABAddressBookRef和CFArrayRef会自动释放吗?

我的其余代码在下面..

CFIndex nPeople = ABAddressBookGetPersonCount(addressBook);
NSMutableArray *List = [[NSMutableArray alloc] init];



    for (int i = 0; i < nPeople; i++) {

        ABRecordRef ref = CFArrayGetValueAtIndex(allPeople, i);
        NSNumber *recordId = [NSNumber numberWithInteger:ABRecordGetRecordID(ref)];

        CFStringRef firstName = ABRecordCopyValue(ref, kABPersonFirstNameProperty);
        CFStringRef lastName = ABRecordCopyValue(ref, kABPersonLastNameProperty);

        CFDataRef imgData = ABPersonCopyImageData(ref);

        ABMutableMultiValueRef multi = ABMultiValueCreateMutable(kABMultiStringPropertyType);

        multi = ABRecordCopyValue(ref,kABPersonEmailProperty);


            for (CFIndex i = 0; i < ABMultiValueGetCount(multi); i++) {

                CFStringRef email, emailLabel;
                emailLabel = ABMultiValueCopyLabelAtIndex(multi, i);
                email      = ABMultiValueCopyValueAtIndex(multi, i);
                NSString *emails = [NSString stringWithFormat: @"%@", (NSString *)email];

                if (emails)
                {
                        NSMutableDictionary *addDict = [[NSMutableDictionary alloc] init];
                        [addDict addObject:emails forKey:@"email"];
                        [List addObject:addDict];
                        [addDict release];
                }


                CFRelease(email);
                CFRelease(emailLabel);

            }

        if (firstName) {
            CFRelease(firstName);
        }

        if (lastName) {
            CFRelease(lastName);
        }

        if (imgData) {
            CFRelease(imgData);
        }

        if (ref) {
            CFRelease(ref);
        }

        CFRelease(multi);
    }


//do something with list
[List release];

3 个答案:

答案 0 :(得分:5)

好的,这有效并且不会泄漏

ABAddressBookRef addressBook = ABAddressBookCreate();
CFArrayRef allPeople = ABAddressBookCopyArrayOfAllPeople(addressBook);
CFIndex nPeople = ABAddressBookGetPersonCount(addressBook);
NSMutableArray *List = [[NSMutableArray alloc] init];

    for (int x = 0; x < nPeople; x++) {

        ABRecordRef ref = CFArrayGetValueAtIndex(allPeople, x);
        ABMultiValueRef emailMultiValue = ABRecordCopyValue(ref, kABPersonEmailProperty);

        NSArray *emailAddresses = [(NSArray *)ABMultiValueCopyArrayOfAllValues(emailMultiValue) autorelease];

        for (int i = 0; i < [emailAddresses count]; i++) {

            NSString *emails = [emailAddresses objectAtIndex:i];

            if (emails)
            {
                    NSMutableDictionary *addDict = [[NSMutableDictionary alloc] init];
                    [addDict addObject:emails forKey:@"email"];
                    [List addObject:addDict];
                    [addDict release];
            }

        }

        if (emailMultiValue)
        {
            CFRelease(emailMultiValue);
        }

        if (ref) {
            CFRelease(ref);
        }

    }

 //do something with list
  [List release];
   CFRelease(allPeople);

答案 1 :(得分:2)

实际上为了消除所有泄漏,您还需要释放ABAddressBookRef - 只需遵循Core Foundation命名准则 - 您使用“ABAddressBookCreate()”获得了引用,并且需要释放任何具有Create或Copy的函数。

答案 2 :(得分:0)

CFErrorRef错误= NULL;

ABAddressBookRef addressbk=ABAddressBookCreateWithOptions(NULL, &error);

if (addressbk != nil)
{

    NSLog(@"Succesful.");
    NSArray *allContacts=(NSArray *)ABAddressBookCopyArrayOfAllPeople(addressbk);  //type casting nsarray from CFArrayRef
    NSUInteger i = 0;
    for (i = 0; i < [allContacts count]; i++)
    {
        Category *mCategory = [[Category alloc]init];
        ABRecordRef contactPerson = ( ABRecordRef)allContacts[i];
        NSString *firstName = ( NSString *)ABRecordCopyValue(contactPerson, kABPersonFirstNameProperty);
        NSString *lastName =  ( NSString *)ABRecordCopyValue(contactPerson, kABPersonLastNameProperty);
        NSString *fullName = [NSString stringWithFormat:@"%@ %@", firstName, lastName];

        mCategory.firstName = firstName;
        mCategory.lastName = lastName;
        mCategory.fullName = fullName;
        //email
        ABMultiValueRef emails = ABRecordCopyValue(contactPerson, kABPersonEmailProperty);

        NSUInteger j = 0;
        for (j = 0; j < ABMultiValueGetCount(emails); j++)
        {
            NSString *email = ( NSString *)ABMultiValueCopyValueAtIndex(emails, j);
            if (j == 0)
            {
                mCategory.homeEmail = email;
                NSLog(@"mCategory.homeEmail = %@ ", mCategory.homeEmail);
            }

            else if (j==1)
                mCategory.workEmail = email;
        }

        [self.arrAddress addObject:mCategory];
        [mCategory release];
        mCategory=nil;
    }
}

CFRelease(addressbk);