2012-10-08 30 views
1

我在顯示mapview時有以下一段代碼。使用ABRecordCopyCompositeName時的內存問題

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]; 

在分析項目來檢查內存泄漏,我得到了警告,「存儲到compositeName對象的潛在泄漏」。我應該釋放string類型的compositeName。

回答

0

這裏對象分配和存儲到「compositeName」具有+保留計數1。我認爲你只需要在這種情況下釋放compositeName。

0

這是回答類似於我answer

請嘗試以下代碼從電話簿受到人們的所有信息

- (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; 

} 
+0

你可以更具體。我面臨的問題是由ABRecordCopyCompositeName引起的內存問題。我需要知道我是否應該在代碼中釋放compositeName變量。 –