0

当我使用构建和分析时,我得到了泄漏(它显示为对象的潜在泄漏)。修复我包括如下

if ( aContactfirstName){
  CFRelease(aContactfirstName);
  }
if (aContactLastName){
  CFRelease(aContactLastName);
  }

但是我的应用程序崩溃了。

所以请让我知道它在哪里泄漏并解决它。

-(NSString*)getContactNameByPhoneNo:(NSString*)phoneNO{

      NSString *aContactName = phoneNO;
      ABAddressBookRef addressbook = ABAddressBookCreate();
      CFArrayRef allPeople = ABAddressBookCopyArrayOfAllPeople(addressbook);
      CFIndex numPeople = ABAddressBookGetPersonCount(addressbook);
      for (int i=0; i < numPeople; i++) { 
        ABRecordRef person = CFArrayGetValueAtIndex(allPeople, i);
        ABMutableMultiValueRef phonelist = ABRecordCopyValue(person, kABPersonPhoneProperty);
        CFIndex numPhones = ABMultiValueGetCount(phonelist);

        for (int j=0; j < numPhones; j++) {
          CFTypeRef ABphone = ABMultiValueCopyValueAtIndex(phonelist, j);
          NSString *personPhone = (NSString *)ABphone;
            NSLog(@"i am:");
          personPhone =[personPhone stringByReplacingOccurrencesOfString:@"-"withString:@""];
          personPhone=[personPhone stringByReplacingOccurrencesOfString:@")"withString:@""];
          personPhone=[personPhone stringByReplacingOccurrencesOfString:@" "withString:@""];
          personPhone=[personPhone stringByReplacingOccurrencesOfString:@"("withString:@""];
          personPhone=[personPhone stringByReplacingOccurrencesOfString:@"+"withString:@""];
            NSLog(@"xcxcxcxc");

          CFRelease(ABphone);

          if ( [personPhone isEqualToString:phoneNO] ){
            NSString *aContactfirstName = (NSString *)ABRecordCopyValue(person, kABPersonFirstNameProperty) ;
            NSString *aContactLastName = (NSString *)ABRecordCopyValue(person, kABPersonLastNameProperty) ;
            if ( aContactLastName != NULL && aContactfirstName != NULL){
              aContactName = [NSString stringWithFormat:@"%@ %@",aContactfirstName,aContactLastName];
            }
            else if(aContactfirstName != NULL){
              aContactName = aContactfirstName;
            }
            else if(aContactLastName != NULL){
              aContactName = aContactLastName;
            }

              if ( aContactfirstName){
                CFRelease(aContactfirstName);
              }
              if (aContactLastName){
                CFRelease(aContactLastName);
              }

            break;
          }
        }
        CFRelease(phonelist);
      }
      CFRelease(allPeople);
      CFRelease(addressbook);
      return aContactName;
    }
4

2 回答 2

0
if(aContactLastName != NULL){
              aContactName = aContactLastName;//aContactName pointing to aContactLastName
            }  

在此,您将 aContactLastName 分配给 aContactName(aContactLastName 和 aContactName 指向相同的内存位置)。之后您将发布aContactLastName。

if (aContactLastName){
                CFRelease(aContactLastName);
              }

然后您返回return aContactName;(aContactName 已发布)这是错误的。

消除

if (aContactLastName){
                CFRelease(aContactLastName);
              }

从您的代码中返回[aContactName autorelease];

于 2012-05-12T09:20:07.977 回答
0

利用 -

NSString *aContactfirstName = [(NSString *)ABRecordCopyValue(person, kABPersonFirstNameProperty) autorelease];
NSString *aContactLastName = [(NSString *)ABRecordCopyValue(person, kABPersonLastNameProperty) autorelease];
于 2012-05-12T08:34:17.247 回答