1

我在显示地图视图时有以下代码。

NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];

MKCoordinateRegion region = mapView.region;
MKCoordinateSpan span;
span.latitudeDelta=100;
span.longitudeDelta=100;
NSString *compositeName =(NSString*)ABRecordCopyCompositeName(person);
if([compositeName length] < 1){

    firstName = @"No Name";
    lastName = @"";
}
else if([firstName length] < 1 && [lastName length] < 1){

    firstName = compositeName;
    lastName = @"";
}
else if([firstName length] < 1)
{
    firstName = lastName;
    lastName = @"";
}

addAnnotation = [[[MyAnnotation alloc] initWithCoordinate:mapCenter title:firstName SubTitle:lastName ]autorelease];
[mapView addAnnotation:addAnnotation];
region.span=span;
region.center=mapCenter;

[pool release];

在分析项目以检查内存泄漏时,我收到警告“存储到复合名称中的对象可能泄漏”。我应该释放字符串类型的复合名称。

4

2 回答 2

0

这是与我的答案相似的答案

请尝试以下代码以从电话簿中获取人员的所有信息

- (BOOL)peoplePickerNavigationController:(ABPeoplePickerNavigationController *)peoplePicker shouldContinueAfterSelectingPerson:(ABRecordRef)person
{
    ABAddressBookRef addressBook = ABAddressBookCreate();

    int i;
    NSString *strName = @"";
    NSString* company = @"";
    NSString *address = @"";
    NSString *suburb = @"";
    NSString *postalcode = @"";
    NSString *state = @"";
    NSString *country = @"";
    NSString *mobile = @"";
    NSString *phone = @"";
    NSString *emailid = @"";


    strName = (NSString *)ABRecordCopyCompositeName((ABRecordRef) person);
    CFStringRef name = ABRecordCopyCompositeName((ABRecordRef) person);
    company  = (NSString *)ABRecordCopyValue((ABRecordRef) person, kABPersonOrganizationProperty);

    NSArray*  allPeople = (NSArray *)ABAddressBookCopyPeopleWithName(addressBook,name);
    CFRelease(name);

    for (i = 0; i < [allPeople count]; i++)
    {
        ABRecordRef record = [allPeople objectAtIndex:i];

        ABMutableMultiValueRef multiValue = ABRecordCopyValue(record, kABPersonAddressProperty);
        for(CFIndex i=0; i<ABMultiValueGetCount(multiValue); i++)
        {
            NSString* HomeLabel = (NSString*)ABMultiValueCopyLabelAtIndex(multiValue, i);
            if([HomeLabel isEqualToString:@"_$!<Home>!$_"])
            {
                CFDictionaryRef dict = ABMultiValueCopyValueAtIndex(multiValue, i);
                address = [NSString stringWithFormat:@"%@", CFDictionaryGetValue(dict, kABPersonAddressStreetKey)];
                suburb = [NSString stringWithFormat:@"%@", CFDictionaryGetValue(dict, kABPersonAddressCityKey)];
                postalcode = [NSString stringWithFormat:@"%@", CFDictionaryGetValue(dict, kABPersonAddressZIPKey)];
                state = [NSString stringWithFormat:@"%@", CFDictionaryGetValue(dict, kABPersonAddressStateKey)];
                country = [NSString stringWithFormat:@"%@", CFDictionaryGetValue(dict, kABPersonAddressCountryKey)];

                CFRelease(dict);
            }
            CFRelease(HomeLabel);
        }
        CFRelease(multiValue);
    }
    CFRelease(allPeople);


    ABMultiValueRef phones =(NSString*)ABRecordCopyValue(person, kABPersonPhoneProperty);
    NSString* mobileLabel = nil;
    for(CFIndex i = 0; i < ABMultiValueGetCount(phones); i++)
    {
        mobileLabel = (NSString*)ABMultiValueCopyLabelAtIndex(phones, i);
        if([mobileLabel isEqualToString:(NSString *)kABPersonPhoneMobileLabel])
        {
            mobile = (NSString*)ABMultiValueCopyValueAtIndex(phones, i);
            NSLog(@"phone   %@",mobile);
        }
        else if ([mobileLabel isEqualToString:(NSString*)kABPersonPhoneIPhoneLabel])
        {
            phone = (NSString*)ABMultiValueCopyValueAtIndex(phones, i);
            NSLog(@"phone   %@",phone);

            CFRelease(mobileLabel);
            break ;
        }
        CFRelease(mobileLabel);

    }
    CFStringRef value, label;
    ABMutableMultiValueRef multi = ABRecordCopyValue(person, kABPersonEmailProperty);
    CFIndex count = ABMultiValueGetCount(multi);
    if (count == 1)
    {
        value = ABMultiValueCopyValueAtIndex(multi, 0);
        emailid = (NSString*) value;
        NSLog(@"self.emailID   %@",emailid);
        CFRelease(value);
    }
    else
    {
        for (CFIndex i = 0; i < count; i++)
        {
            label = ABMultiValueCopyLabelAtIndex(multi, i);
            value = ABMultiValueCopyValueAtIndex(multi, i);

            // check for Work e-mail label
            if (CFStringCompare(label, kABWorkLabel, 0) == 0)
            {
                emailid = (NSString*) value;
                NSLog(@"self.emailID   %@",emailid);
            }
            else if(CFStringCompare(label, kABHomeLabel, 0) == 0)
            {
                emailid = (NSString*) value;
                NSLog(@"self.emailID   %@",emailid);
            }

            CFRelease(label);
            CFRelease(value);
        }
    }
    CFRelease(multi);

        }


    CFRelease(phones);
    CFRelease(addressBook);
    [self dismissModalViewControllerAnimated:YES];

    return NO;

}
于 2012-10-08T09:36:47.973 回答
0

这里分配并存储到“compositeName”中的对象的保留计数为+1。我认为你只需要在这种情况下释放compositeName。

于 2012-10-10T11:05:53.053 回答